How to Integrate Google reCAPTCHA with PHP

Google reCaptcha 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 properly setup then it shows a 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
  3. Conclusion

1. Register website

  • First, need to register the website to get the site and secret key. For this go to the following link and click on Get reCAPTCHA button from the top right.
  • It will ask you to login if you are not logged in otherwise it directly takes you to the Admin page.
  • Now go to the Register a new site section.

recaptcha admin

  • In first text box type your domain name ( for example – demo.makitweb.com ) and in another input box type again your domain name or you may type multiple domains name like makitweb.com, demo.makitweb.com, etc. and click on Register button.
  • You get your site and secret key.

demo.makitweb.com site and secret key generated are –

Site key is 6LdVCBoTAAAAAObDKfGUy07iM4NQ4Yi2c4sNYnzD and

Secret key is 6LdVCBoTAAAAAImaReEinReLCYHfunE7Gnfh7xAl

In this page, you will also see

Client side integration

<script> and <div> with class="g-recapthca" that use in next step.


2. Integrate

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

HTML (Completed Code)

<html>
   <head>
      <title>Makitweb</title>
      <link href="style.css" rel="stylesheet" type="text/css" />
      <script src='https://www.google.com/recaptcha/api.js'></script>
   </head>
   <body>

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

            <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>Subject</div>
               <input type="text" id="txt_subject" name="txt_subject">

               <div>Message</div>
               <textarea></textarea>

               <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>

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";
}

Complete Code

$secretkey = "6Le58hkTAAAAAHnhkm5Ww2aSpT9hz97J5_5qHnmJ"; // Secret Key

$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';
  }

}

3. Conclusion

With 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.

Related Post

Spread the love

Be First to Comment

Leave a Reply

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