Google reCaptcha PHP Code Example Script

Google has released the new reCAPTCHA. One of my reader request to me for a tutorial post about Google reCaptcha PHP code example script. First of all we need to know what is php captcha or reCaptcha before using it in our application. In one word using reCAPTCHA users can prove they are human. They need just a single click to confirm they are not a robot. So, reCAPTCHA will protect your website from spam or robots with better user experience. You can easily integrate Google reCAPTCHA in PHP script. Here we discussed about Google reCaptcha PHP code with example script also in this demo example give idea about php reCaptcha validation after form submit. You can easily download for free from Download source link. I like the design it is clean and impressive, hope you will also like it.

We have created a contact form with the Google reCAPTCHA using PHP. Take a look at the demo of Google reCAPTCHA in PHP from the Demo link. The reCAPTCHA Google integration process is given below. So clickers who are just in beginner stage in web application can get an clear idea about Google reCaptcha, PHP captcha code, captcha php, how to integrate google reCaptcha in php, how to validate reCaptcha in php.

Take a quick look at this live demo. Also you can free download this Google reCaptcha php code here and can try in your localhost or live server where you want to do.

Learn how to integrate google reCaptcha using PHP

Step 1: First we need Google reCaptcha Key
you need to register your site and get reCAPTCHA API keys.

Step 2: Register your site
Register your site at Google from here – https://www.google.com/recaptcha/admin
You have to fill up the below form about register a new site.
register new site for google recaptcha
After filling you will get your Site Key and secret key.

Google Site Key :
You will use this in HTML code.
 google-recaptcha-site-key

Google Secret Key :
This will help your website to communication with Google.
google-recaptcha-secret-key

HTML Code:
At first you need to include the reCAPTCHA API JavaScript library like other library very common example like min.js. And after that just place your html form.
 google-recaptcha-client-side-integration

<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<form action="" method="POST">
    <input type="text" name="name" value="" />
    <textarea type="text" name="message"></textarea>
    <div class="g-recaptcha" data-sitekey="6LeycSQTAAAAAMNe6P3VweDABToZ1h4CO6dUqc48"></div>
    <input type="submit" name="submit" value="SUBMIT">
</form>

PHP Code for reCaptcha Validation:
Into the PHP code you need to modify Google Secret Key.

<?php
if(isset($_POST['submit']) && !empty($_POST['submit'])):
    if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])):
        //your site secret key
        $secretKey = '6LeycSQTAAAAAMM3AeG62pBslQZwBTwCbzeKt06V';
        //get verify response data
        $verifydata = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secretKey.'&response='.$_POST['g-recaptcha-response']);
        $response= json_decode($verifydata);
        if($response->success):
            //contact form submission code
            $name = !empty($_POST['name'])?$_POST['name']:'';
            $message = !empty($_POST['message'])?$_POST['message']:'';
            $success = 'Your contact form have submitted successfully.';
        else:
            $error = 'Robot verification failed, please try again.';
        endif;
    else:
        $error = 'Please select Google reCAPTCHA.';
    endif;
else:
    $error = '';
    $success = '';
endif;
?>

In this way we can easily integrate Google reCaptcha or Google captcha with php. So basically if your query about php captcha, reCaptcha php, Google reCaptcha integration using php and how to validate Google reCaptcha using php i think this demo example will be very helpful for you. So, don’t miss to learn and practice yourself. Thanks for watching this tutorial. You can try yourself and share with your friends. I will share some useful script in PHP as well as some tips and tricks in PHP programming. Viewers can subscribe our newsletter to get all the updates about web programming. Share and likes will be appreciated also help to evaluate our self.
Support
If you need any help regarding the same please feel free to comment we love to help you.

4 responses to “Google reCaptcha PHP Code Example Script”

  1. Enoch says:

    I’m having issues integrating the recaptcha with this code. Please take a look at it and help me. Thanks.

  2. Roshan says:

    Thanks for sharing this. This is a simple & easy integration technique. Keep up the good work.

  3. Jacob says:

    Great job this was easy to get working. I added a quick user experience improvement by replacing action=”” with action=”#form-anchor” so it takes you down to the form instead of the top of the page. Can you also please describe how to get this to email?

Leave a Reply

Your email address will not be published. Required fields are marked *