reCAPTCHA is extremely important these days to verify bots aren’t trying to take advantage of your site. reCAPTCHA 2.0 is a little bit different than the old reCAPTCHA. You are no longer typing text into that box, it’s simply a click of a button.

reCAPTCHA with PHP

 

The method of verifying users is a bit different. “g-recaptcha-response” is sent through a POST request to your server-side script, and along with your reCAPTCHA key that you get when you sign up, you pass that along to Google to tell if the user is a bot or not. The response is a JSON response so we will be using file_get_contents() and json_decode() to fetch the response and decide what to do from there with our script.

The following is a simple HTML form you would most likely see on a common contact form. The div with the class of “g-recaptcha” is where we are actually placing our captcha. You must replace data-sitekey with the key you get from google when you sign up (https://www.google.com/recaptcha/intro/index.html).

HTML FORM

PHP Server-Side Script

If you want to see how to set up reCAPTCHA 2.0 with AJAX, click the link.