Google introduce new reCaptcha technology through which we prevent site from roboot 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 roboot. In this post i explain 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 new google account. Then go to reCaptcha admin area.

Step 2. Now register your site for reCaptcha.

 Step 3.  After register your site on google reCaptcha get your site key and security key. We need them for implementation of google reCaptcha in 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

At server side first get the captcha code from form then we need to verify captcha by sending data to google server to validate i am using PHP so i use CURL  to do that and get JSON response from 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>