How to Integrate Google reCAPTCHA v2 with PHP

Google reCaptcha is generally used to protect the website from spammers. It is best and easy way to show CAPTCHA within the website.

It is easy to integrate into the form and when it is properly setup then it shows an I’m not a Robot Checkbox that shows different types of images on the screen when it gets clicked.

The user needs to select images according to the asked question.

How to Integrate Google reCAPTCHA with PHP


Contents

  1. Register website
  2. Integrate Google reCAPTCHA
  3. Demo
  4. Conclusion

1. Register website

  • Navigate to the following link and login into your Gmail account if not logged in.

How to Integrate Google reCAPTCHA with PHP

  • Enter label, select reCAPTCHA v2 from the reCAPTCHA type, and select "I'm not a robot" Checkbox.
  • Enter your domain names e.g. makitweb.com.
  • Check the Accept the reCAPTCHA Terms of Service and click the Submit button.

How to Integrate Google reCAPTCHA with PHP

  • Copy the site and secret keys.

2. Integrate Google reCAPTCHA

  • Add the <script src='https://www.google.com/recaptcha/api.js'></script> in the <head> section.
  • Create a <form > and add the <div class="g-recaptcha" data-sitekey="6Le58hkTAAAAAJuIL3-S1ZASBRtoQbpqThw0BJLz"></div>. Here, the data-sitekey has the site key value.

PHP

When the submit button is getting clicked then, pass the secret key, 'g-recaptcha-response' response and remote IP to file_get_contents method.

$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretkey."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);

After that checking g-recaptcha-response POST or not. If it is not then displayed Verify Captcha message. Otherwise, decode the JSON response and check its success or not.

$responseData = json_decode($response);
if($responseData->success) {
   $msg = "Verified";
}

Completed Code

<html>
  <head>
    <title>How to Integrate Google reCAPTCHA v2 with PHP</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
    <script src='https://www.google.com/recaptcha/api.js'></script>

    <?php
    $secretkey = "6Ld0O9caAAAAANtaR18R_wUYyxdG8FcExIhvDJSV";
    $msg = '';
    if(isset($_POST['submit'])){
       $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretkey."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);

       if(($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])) {

          $responseData = json_decode($response);

          if($responseData->success) {
             $msg = 'Verified';
          }

       }else{
          $msg = 'Verify Captcha';
       }
    }
    ?>
 </head>
 <body>

    <div class="container">
       <form method='post' action=''>
          <h2>Google reCAPTCHA</h2>

          <div class="content">
             <div><h2><?php echo $msg; ?></h2></div>
             <div>Name</div>
             <input type='text' id="txt_name" name="txt_name" />

             <div>Email</div>
             <input type="text" id="txt_email" name="txt_email">

             <div>Message</div>
             <textarea></textarea>
             
             <!-- reCAPTCHA -->
             <div class="g-recaptcha" data-sitekey="6Le58hkTAAAAAJuIL3-S1ZASBRtoQbpqThw0BJLz"></div>

             <div>
                <input type='submit' id='submit' name='submit' value='Submit'>
             </div>
          </div>

       </form>
    </div>

   </body>
</html>

3. Demo

View Demo


4. Conclusion

With the above script, you can integrate Google reCaptcha on your <form > but make sure to generate your own site and secret key and update it on the code.

If you found this tutorial helpful then don't forget to share.

Leave a Comment