Last updated on April 30th, 2021 at 10:21 am

Google introduce new reCaptcha technology through which we prevent the site from the robot and most important feature we don’t need to enter captcha code you need to check the book and google automatic detect that you are human or robot. In this post, I explain to you how to implement google reCaptcha in your website using PHP.

hero-recaptcha-demo

 DEMO

Google No CAPTCHA reCAPTCHA

Step 1. Login to your Google account or create a new google account. Then go to the reCaptcha admin area.

Step 2. Now register your site for reCaptcha.

 Step 3.  After register, your site on google reCaptcha gets your site key and security key. We need them for the implementation of google reCaptcha on our site.

Client side

In html page we need to add google javascript file :

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

and add this div where you want to load captcha

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

 Server Side

On the server-side first get the captcha code from the form then we need to verify the captcha by sending data to the Google server to validate I am using PHP so I use CURL  to do that and get JSON response from the google server

URL

https://www.google.com/recaptcha/api/siteverify?secret=YOUR_SECRET&response=RESPONSE_FROM_FORM

PHP Code

<?php
if(isset($_POST['g-recaptcha-response'])){
	// Send data and get response
	$cap = $_POST['g-recaptcha-response'];
	$ch = curl_init('https://www.google.com/recaptcha/api/siteverify?secret=6Lc4vP4SAAAAAGOM8ERb1pYSBfHjiMGb9bnGVtog&response='.$cap);                                                                      
	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");                                                                                                                               
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
	$result = curl_exec($ch);
	curl_close($ch);
	
	print_r($result);
}
?>

Complete Code:

<html>
<head>
	<title>Captcha Test</title>
	<script src='https://www.google.com/recaptcha/api.js'></script>
</head>

<body>
	<form action="" method="post">
		<div class="g-recaptcha" data-sitekey="6Lc4vP4SAAAAABjhRjyoMguw66mNSBgdpBF398AG"></div>
		<div>
			<input type="submit" value="Submit">
		</div>
	</form>
	
<?php
if(isset($_POST['g-recaptcha-response'])){
	// Send data and get response
	$cap = $_POST['g-recaptcha-response'];
	$ch = curl_init('https://www.google.com/recaptcha/api/siteverify?secret=6Lc4vP4SAAAAAGOM8ERb1pYSBfHjiMGb9bnGVtog&response='.$cap);                                                                      
	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");                                                                                                                               
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
	$result = curl_exec($ch);
	curl_close($ch);
	
	print_r($result);
}
?>
</body>
</html>