You are currently browsing the PHP category

Integrate Google New reCaptcha Using PHP

§ March 7th, 2015 § Filed under PHP § Tagged , , , , , , § No Comments

reCAPTCHA is a free service that protects your site from spam and abuse. It uses advanced risk analysis engine to tell humans and bots apart. With the new API, a significant number of your valid human users will pass the reCAPTCHA challenge without having to solve a CAPTCHA. reCAPTCHA comes in the form of a widget that you can easily add to your blog, forum, registration form, etc.

picHundreds of millions of CAPTCHAs are solved by people every day. reCAPTCHA makes positive use of this human effort by channeling the time spent solving CAPTCHAs into digitizing text, annotating images, building machine learning datasets. This in turn helps preserve books, improve maps, and solve hard AI problems.

Get reCaptcha Key
Click here to create a Google reCaptcha application.

Register site
Provide your domain detail without http:// or https:// (e.g www.tricks9.com) for website registration

tricks9.com

After registration you will get 2 keys

Site key – This will be used in HTML code

Secret Key – This for communication between your site and Google.

 

Create Simple Html Form page 

<html>
<head>
<title>Google reCaptcha</title>
<script src="https://www.google.com/recaptcha/api.js" async="" defer="defer"></script>
</head>
<body>
<form action="index.php" method="POST">
<div class="g-recaptcha" data-sitekey="your_site_key"></div>
<input type="submit" value="Submit" />
</form>
</body>
</html>

 


<?php
if(isset($_POST['submit'])){
$captcha=$_POST['g-recaptcha-response'];
if(!empty($captcha))
{
$errMsg= '';
$google_url="https://www.google.com/recaptcha/api/siteverify";
$secret=_YOUR_SECRET_KEY;
$ip=$_SERVER['REMOTE_ADDR'];
$captchaurl=$google_url."?secret=".$secret."&response=".$captcha."&remoteip=".$ip;

$curl_init = curl_init();
curl_setopt($curl_init, CURLOPT_URL, $captchaurl);
curl_setopt($curl_init, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_init, CURLOPT_TIMEOUT, 10);
$results = curl_exec($curl_init);
curl_close($curl_init);

$results= json_decode($results, true);
if($results['success']){
$errMsg="Valid reCAPTCHA code. You are human.";
}else{
$errMsg="Invalid reCAPTCHA code.";
}
}else{
$errMsg="Please re-enter your reCAPTCHA.";
}
}

?>