Integrate Google New reCAPTCHA with PHP

google recapture
What is google recapture  😉

In this tutorial, we are going to integrate the Google reCAPTCHA in PHP contact form

Google has released the new reCAPTCHA. Now users can prove they are human without solving a CAPTCHA. users need just a single click to confirm they are not a robot. well, reCAPTCHA will protect your website from spam with better user experience. It can be  easily integrate Google reCAPTCHA with PHP script.

We have created a contact form with the new Google reCAPTCHA using PHP. Take a look the example image
reCAPTCHA
Image Credit to www.codedodle.com 

Before you start coding you need to sign up on Google to get your own API credentials
CLICK HERE TO GET RECAPTURE API KEY
reCAPTCHA

WHAT IS SITE KEY & SECRETE KEY
1.Site Key
We need to provide site key to display the reCAPTCHA widget on our custom contact form.

2.SECRET KEY
It used to connect the site and reCAPTCHA Server
reCAPTCHA


Once you’ve signed up you’ll need to add the Javascript to your page between the head tags.

src='https://www.google.com/recaptcha/api.js'

Next you need to add the HTML PART with your public key to the area you want the CAPTCHA to be displayed.

<div class="g-recaptcha" data-sitekey="KEY GOES HERE"></div>

Here is the Source (including reCAPTURE div class)

<form method="post" action="" class="form-group">

      <input type="text" name="myreq" class="form-control" placeholder="Full Name" required style="width: 22%;"><br><br>
      <div class="g-recaptcha" data-sitekey="6LdvBh0TAAAAAPiqwGXs6NW5Zq3TpkpA7ogQP_O_"></div><br><br>
      <input type="submit" name="submit" class="btn btn-success" style="width: 22%; value="Send Request">

    </form>
<?php
if(isset($_POST['submit'])){
  
  $secret = '6LdvBh0TAAAAAOYRhKlzmDz1KmrDRESJiRKgfmRk';
  $response = $_POST['g-recaptcha-response'];
  $remoteip  =  $_SERVER['REMOTE_ADDR'];
  
  $url = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=$response&remoteip=$remoteip");
  $result = json_decode($url, TRUE);
  //print_r($result);
  //die();
  if($result['success']==1){
    echo $_POST['myreq'];
  }
  
}


?>

The following is a simple HTML for most likely see on a common contact form. You must replace data-sitekey with .otherwise it gives you an error.
If you have any questions about the post or if you face any troubles while following the process let me know using the comment section.  😎

 

Sathishka Priyad

Software Engineer | Researcher | Blogger Founder of — Tech Orbit

Leave a Reply

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

x Shield Logo
This Site Is Protected By
The Shield →