Google New reCaptcha using PHP – Are you a Robot?

For the past several years, Google’s reCAPTCHA has verified a user’s humanity by forcing you to decipher warped, nonsensical text. reCAPTCHA protects the websites you love from spam and abuse.

Recently Google introduced a simplified “captcha” verification system (video) that enables users to pass the “captcha” just by clicking on it. This API utilizes an Advanced Risk Analysis engine that is capable of discerning between users and bots. The best part is that the interface has been simplified to a checkbox, a vast improvement over reCAPTCHA’s alphabet soup scramble.

Implement Google Repaptcha

reCaptcha

However, CAPTCHAs aren’t going away just yet. In cases when the risk analysis engine can’t confidently predict whether a user is a human or an abusive agent, it will prompt a CAPTCHA to elicit more cues, increasing the number of security checkpoints to confirm the user is valid.

 

In this post We had implemented new reCaptcha API system with HTML login form using PHP. We hope you will like it. Please take a look on the steps of implementing new reCaptcha API:

New reCaptcha using PHP

1. Get reCaptcha Key

Create a Google reCaptcha application by clicking here

2. Register Your Website

register for google recaptcha

3. Google Site Key :

You will use this in HTML code.

site key

4. Google Secret Key

Key for communicating between your website and Google. secret key

5. Implementation

clientside integration

Integration: Click for large preview

Now implement Google’s Captcha JS in html as follows :

HTML Code

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Google Recaptcha</title>

<script src="https://www.google.com/recaptcha/api.js"></script>
</head>
<body>
<form id="recaptcha" name="recaptcha" action="" method="post">
Email
<input type="text" name="email" id="email" class="input" />
Name
<input type="text" name="name" id="name" class="input" />
Message
<textarea cols="35" rows="4" id="message" name="message" class="input" ></textarea>
<br/>
<br/>
<div class="g-recaptcha" data-sitekey="6LeZcQITAAAAAP9obo-8VbNtneWpE_DRE_CZ6v79"></div>
<br/>
<br/>
<input type="submit" class="button button-primary" value="Submit" />
<span class='msg'><?php echo $msg; ?></span>
</form>
</body>
</html>

It is a simple HTML Form code with Google reCaptcha implementation. Here you have to modify the Google Site Key value (data-sitekey) which you got from Google as specified prior.

HTML Form

PHP Code

In this PHP code, we have to modify the Google Secret Key and we are using CURL, so Enable php_curl extension in php.ini configuration file. You can check CURL enable by printing phpinfo() which outputs of PHP configuration.

<?php
	session_start();
	$msg='';
	if(isset($_POST))
	{
		$recaptcha=$_POST['g-recaptcha-response'];
		if(!empty($recaptcha))
		{
			$google_url="https://www.google.com/recaptcha/api/siteverify";
			$secret='GOOGLE SECRET KEY';
			$ip=$_SERVER['REMOTE_ADDR'];
			$url=$google_url."?secret=".$secret."&response=".$recaptcha."&remoteip=".$ip;

			$curl = curl_init();
			curl_setopt($curl, CURLOPT_URL, $url);
			curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($curl, CURLOPT_TIMEOUT, 10);
			curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16");
			$results = curl_exec($curl);
			curl_close($curl);

			$res= json_decode($results, true);
			if($res['success'])
			{
				/********/
				// Write PHP code to do whatever you want
				/********/
			}
			else
			{
				$msg="Please re-enter your reCAPTCHA.";
			}
		}
		else
		{
			$msg="Please re-enter your reCAPTCHA.";
		}
	}
?>

Now check the form and verify Google New reCaptcha using PHP is working as desired and rest assure the visitor filled the form, is not a robot.

You Might Interested In

1 COMMENT

Leave a Reply

Enclose a code block like: <pre><code>Your Code Snippet</code></pre>.