Introduction to Google reCaptcha

Google reCaptcha is Captcha like system that is used to differentiate between an human and a Robot. Google reCaptcha is use to reduce spam and increase the security of your applications. Most of all traditional captcha is less secure while google recaptcha provide a secure way to reduce spamming. Since it is develop and tested by world’s top developers.

In reCaptcha, system asked user to click on a checkbox to prove that he is an human. The logic behind this is known cookies or mouse movements and if this fails ,it asked user to select the frames of images of a specific object. For example frames of images seems like car, shop etc.

Google reCaptcha gif image

Why use Google reCaptcha ?

How to integrate Google reCaptcha ?

Let’s start with Google reCaptcha.

Step 1: First of all we need an api key. We can get the api key from https://www.google.com/recaptcha/admin . To get an api key, you must logged in with your google account. After login you can register your site name here.

Google reCaptcha registration form

Add a label for your site like i have added Happycoder and Domains section add your site name for example happycoder.me and click on register.

Step 2: After registering you will get the follwing keys.

Google reCaptcha security key

Step 3: Now include the following script in head section of your page

 

Step 4: The next thing is to add the following placeholder inside your form tag

 

Note: data-sitekey="Your data site key is here provide by google"

Some other attribute that you can use like data-themedata-type, data-size etc.

Step 5:  Now writing all code together

Step 1: 

index.php

 

Example form

Step 2: We have a form with method post and action form_submit.php

form_submit.php

 

as a result you will get the following hashed value, If you submit the after checking the captcha .

Example page

If you not check the captcha you will see the g-recaptcha-response value is null.

Step 3:  Download the recaptchalib.php file from github and include it into form_submit.php file

 

Step 4: recaptchalib.php library contains a lot of functions that sends g-recaptcha-response to google by using a HTTP Request.

To use these functions we will set some variables

 

Here we will check that form is submitted with or without reCaptcha.

 

if it submitted with google reCaptcha it display message to you otherwise redirects you to your index.php file.

 

The final form_submit.php file with all above code is as follow

 

You can also read Google place autocomplete address api

after submitting the form successfully, we get the following message otherwise redirect to index.php page.


Guys if you feel any problem in google reCaptcha integration, you can also leave it on comment box.

Thanks.