Google reCAPTCHA using PHP

Google reCAPTCHA is a free API service provided by Google itself. It protects your website from spam and abuses. It checks whether a request is arriving from a human being or from a web robot. Now a days most of the website uses google reCAPTCHA in their website to reduce unwanted traffic from automated robots. You can integrate google reCAPTCHA in your online forms like – user registration form, contact form and in many other forms. So if you integrate google reCAPTCHA then you do not require to develop a system for validating captcha for your website. It is very easy to integrate Google reCAPTCHA in you website. In this case we will be showing you how to integrate Google reCAPTCHA in a user registration form.

google recaptcha

Get reCAPTCHA API keys from Google: For getting a reCAPTCHA API key for your website you have to register your site to google.

Click the URL to register your website at Google. (if you are not logged in just login to your account)

google recaptcha registration form

Fillup the form and click on Register. You will find your two keys. One is Site key and another one is Secret key.

Site key: You will add this key in your website form.

Secret key: This key will reside in your server side code. As shown in the following example code. Use this for communication between your site and Google. Be sure to keep it a secret.


google recaptcha api keys


Add the following like in the head section of your registration page:

<script src=''></script>


Add your site key in the form as follows:

<form method="post">

          User Name:<input type="text" id="txtName" name="txtName"/>

          Email ID:<input type="email" id="txtEmail" name="txtEmail"/>

          Mobile No:<input type="text" id="txtMobNo" name="txtMobNo"/>

          Password:<input type="password" id="txtPass" name="txtPass"/>

          Conform Password:<input type="password" id="txtConPass" name="txtConPass"/>

<div class="g-recaptcha" data-sitekey="put your site key here"></div>

         <input type="submit" id="btnReg" name="btnReg" value="Submit"/>



Server side integration: Server side code for verifying your registration information


if (isset($_POST['btnReg']) && htmlspecialchars($_POST['btnReg']) === 'Submit') {

    if (isset($_POST['txtName']) ) {

        $details['name'] = $_POST['txtName'];

    } else {

        $error['name'] = "Invalid Username";


    if (isset($_POST['txtEmail'])) {

        $details['email'] = $_POST['txtEmail'];

    } else {

        $error['email'] = "Invalid Email";


    if (isset($_POST['txtMobNo'])) {

        $details['txtMobNo'] = $_POST['txtMobNo'];

    } else {

        $error['txtMobNo'] = "Invalid Mobile No";


    if (isset($_POST['txtPass'])) {

        if ($_POST['txtPass'] === $_POST['txtConPass']) {

            $details['password'] = $_POST['txtPass'];

        } else {

            $error['txtConPass'] = "Password Missmatch";


    } else {

        $error['txtPass'] = "Invalid Password";


    if (check all errors && isset($_POST['g-recaptcha-response']) {

     $secret = 'your secret key';

        //get verify response data

        $response = file_get_contents(''.$secret.'&response='.$_POST['g-recaptcha-response']);

        $responseData = json_decode($response);


         // Call to Save function to Save your registration information


         echo ‘Invalid information.’
Check Page Rank