How to Add Google reCAPTCHA in PHP form

This is a really quick tutorial to add Google reCAPTCHA in a PHP form. reCAPTCHA is used to prevent spam or autonomous illegal activities on your website, which can result in overuse of the bandwidth of your server fill your databases with junk data.

Here we will discuss v2 of Google reCAPTCHA.

Let’s get started…

Also Check: Add reCAPTCHA v2 and Form Validation using Custom jQuery

Step 1) Go to Google reCAPTCHA , then click on “My reCAPTCHA” after sign up, then Add a label, select the first option “reCAPTCHA v2” add your site URL then click on Register

 

Step 2) Next screen will show your “Site key” and “Secret key” keep them safe, we will need it later.

 

Step 3) Now go to this URL to download the source code, in downloaded zip upload “src” folder to your same server with form page.

Step 4) Open form.php which will have your form and reCAPTCHA code. Now add below javascript file in your form.php ‘s head section

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

Add below HTML from Step 2) in your form where you want to show reCAPTCHA

<div class="g-recaptcha" data-sitekey="6Lc6OmIUAAA3kwehio34yb5Vwroq"></div>

Then add this PHP code in head section of form.php

<?php

require('src/autoload.php');

$siteKey = '6LegPmIUAAAAADLwDmXXXXXXXyZAJVJXXXjN';
$secret = '6LegPmIUAAAAAO3ZTXXXXXXXXJwQ66ngJ7AlP';

$recaptcha = new \ReCaptcha\ReCaptcha($secret);

$gRecaptchaResponse = $_POST['g-recaptcha-response']; //google captcha post data
$remoteIp = $_SERVER['REMOTE_ADDR']; //to get user's ip

$recaptchaErrors = ''; // blank varible to store error

$resp = $recaptcha->verify($gRecaptchaResponse, $remoteIp); //method to verify captcha
if ($resp->isSuccess()) {
   // send mail or insert in db or do whatver you wish to
} else {
   $recaptchaErrors = $resp->getErrorCodes(); // set the error in varible
}


?>

Add “site key” and “secret key” in above code from step 2

Now Complete HTML of form.php will look like this

<!doctype html>
<html lang="en">

<head>
    <title>FreakyJolly.com Google reCAPTCHA EXAMPLE form</title>
    <script src='https://www.google.com/recaptcha/api.js'></script>
</head>

<body>
    <?php

	require('src/autoload.php');

	$siteKey = '6LegPmIUAAAAADLwDmXXXXXXXyZAJVJXXXjN';
	$secret = '6LegPmIUAAAAAO3ZTXXXXXXXXJwQ66ngJ7AlP';

	$recaptcha = new \ReCaptcha\ReCaptcha($secret);

	$gRecaptchaResponse = $_POST['g-recaptcha-response']; //google captcha post data
	$remoteIp = $_SERVER['REMOTE_ADDR']; //to get user's ip

	$recaptchaErrors = ''; // blank varible to store error

	$resp = $recaptcha->verify($gRecaptchaResponse, $remoteIp); //method to verify captcha
	if ($resp->isSuccess()) {

	   /******** 
	   
		Add code to create User here when form submission is successful
	   
	   *****/
	   
	} else {
		
		/****
		
		// This variable will have error when reCAPTCHA is not entered correctly.
		
		****/

	   $recaptchaErrors = $resp->getErrorCodes(); 
	}


	?>
        <form autcomplete="off" class="form-createuser" name="create_user_form" action="" method="post">
            <div class="panel periodic-login">
                <div class="panel-body text-center">
                    <div class="form-group form-animate-text" style="margin-top:40px !important;">
                        <input type="text" autcomplete="off" class="form-text" name="new_user_name" required="">
                        <span class="bar"></span>
                        <label>Username</label>
                    </div>
                    <div class="form-group form-animate-text" style="margin-top:40px !important;">
                        <input type="text" autcomplete="off" class="form-text" name="new_phone_number" required="">
                        <span class="bar"></span>
                        <label>Phone</label>
                    </div>
                    <div class="form-group form-animate-text" style="margin-top:40px !important;">
                        <input type="password" autcomplete="off" class="form-text" name="new_user_password" required="">
                        <span class="bar"></span>
                        <label>Password</label>
                    </div>
                    <?php 
							if(isset($recaptchaErrors[0])){
								print('Error in Submitting Form. Please Enter reCAPTCHA AGAIN');
							}
					  ?>
                    <div class="g-recaptcha" data-sitekey="6LegPmIUAAAAADLwDmmVmXXXXXXXXXXXXXXjN"></div>
                    <input type="submit" class="btn col-md-12" value="Create User">
                </div>
            </div>
        </form>
</body>

</html>

 

 

Leave a Comment

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