How to Integrate google reCAPTCHA with PHP [ With Example ]

By: Sunil Kumar |  In: PHP  |  Last Updated: 2018/07/26

Spamming is one of the most common problem on the web. Every site owner wants to get rid of spamming whether it is spam traffic or spam messages or spam comments on your site.

To get rid of spamming you need to put some checks on your website. Previously people used to put some random number or string on the screen and verify it on the server side.

But this method is old days method. Google’s reCAPTCHA is a new way to verify a user. And it is very simple to verify the user. They just need a single click or tap to prove they are not a robot.

google reCAPTCHA

In this guide, you will learn how to integrate google reCAPTCHA on your website with PHP step by step.

Step 1: Get reCAPTCHA API key

You need to register your website and get an API key from google to use reCAPTCHA.

Register your website at –


Choose the type of captcha you want to use and the domain/domains for you will use this API key and click on the Register Button.

Now you will get your site key and secret key.

reCAPTCHA secret key and site key

Step 2: Adding reCAPTHA to your site

To add reCAPTCHA first you need to include reCAPTCHA JavaScript library in your HTML

<script src='' async defer >

Now add this HTML code where you want to show the reCAPTCHA widget.

<div class="g-recaptcha" data-sitekey="your_site_key">

Replace your_site_key with the actual site key provided by the google.

Once you are done refresh your page and you will see the reCAPTCHA widget on your site.

Step 3: Validate the user response

You need to validate the response when a user click on the reCAPTCHA widget. To verify you need to write some PHP code as below

  if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response']))
        $secret = 'your_actual_secret_key';
        $verifyResponse = file_get_contents(''.$secret.'&response='.$_POST['g-recaptcha-response']);
        $responseData = json_decode($verifyResponse);
            $succMsg = 'Your contact request have submitted successfully.';
            $errMsg = 'Robot verification failed, please try again.';

Add your actual secret key in the above code and you are Done.

Feel free to write a comment if you face any problem. Will be happy to help you as soon as possible.


Leave a Comment

Your email address will not be published.


Sunil Kumar

I am the owner of acmeextension. I am a passionate writter and reader. I like writting technical stuff and simplifying complex stuff.
Know More

Join more than 10,000 others Web Developers

%d bloggers like this: