google recaptcha with php

Google release the new reCAPTCHA. Using new Google reCAPTCHA users can now attest they are human without having to solve a CAPTCHA. They just need to single a click to confirm they are not a robot. So, Google reCAPTCHA validate the human users and protect your site from spam/bot with the better user experience.

In this tutorial, we will show you how to integrate Google reCAPTCHA with php.We have a created a contact form with the new Google reCAPTCHA using php.Take a look the demo of Google reCAPTCHA in php.

View Demo

The Google reCAPTCHA integration process is given below:

google recaptcha

Google reCAPTCHA API keys:

For Google reCAPTCHA integration to your site, you need to create reCAPTCHA API keys for your website.

First, go to this link and create reCAPTCHA API keys:  https://www.google.com/recaptcha/admin

Google reCAPTCHA

Get your reCAPTCHA Site & Secret Keys:

Site key is used to display the reCAPTCHA on your site. Secret key helps authorizes communication between your site and the reCAPTCHA server.

google recaptcha keys

Code:

At first, you need to include the reCAPTCHA API JavaScript library before the closing </head> tag on your HTML page.

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

Paste this following code at the end of the <form> where you want to display the reCAPTCHA widget.

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

Before the Submit button, we have placed the Google reCAPTCHA widget div.

<form action="" method="POST">
    <input type="text" name="name" value="" />
    <input type="text" name="email" value="" />
    <textarea type="text" name="message"></textarea>
    <div class="g-recaptcha" data-sitekey=" your recaptcha site key"></div>
    <input type="submit" name="submit" value="SUBMIT">
</form>

PHP code:

In this PHP code, you need to modify Google reCAPTCHA Secret key.

<?php
// when form is submitted
if(isset($_POST['submit']) && !empty($_POST['submit'])){ 

    if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){ 
        
        //your site secret recaptcha key
        $secret = 'YOUR-SITE-SECRET-KEY';
 
         
        //get verify reCAPTCHA response data
        $recaptcha = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
        
        $response = json_decode($recaptcha); 
        
        // check was the response successfully checked by Google reCAPTCHA
        if($response->success){
          	// if recaptcha check was success
            echo 'Your contact request have submitted successfully.';
            
        }else{
        	// if detect bot show the error
            echo 'Robot verification failed, please try again.';
        }
         
    }else{
    	// if recaptcha is not checked
        echo 'Please click on the reCAPTCHA box.';
    } 
}
?>

Are you want to get Google reCAPTCHA implementation help, or modify or extend the functionality of this script? Click Here

LEAVE A REPLY

Please enter your comment!
Please enter your name here