Today Google has been introduced new reCaptcha API called Are you a robot? “No Captcha reCaptcha” a complete new design captcha system. This protects your website for spammers and robots, in this post I had implemented new reCaptch API system with HTML login form using PHP. I like the new design it is clean and impressive, hope you will like it. Please take a look quick look at the demo.
Download Script Live Demo
Get reCaptcha Key
Click here to create a Google reCaptcha application.
Register Your Website
Give your website domain details without http:
Google Site Key
You will use this in HTML code.
Google Secret Key
This will help your website to communication with Google.
HTML Code
Contains simple HTML code with Google reCaptcha widget snippet. Here you have to modify the Google Site Key value.
<html>
<head>
/* Google reCaptcha JS */
<script src="https://www.google.com/recaptcha/api.js"></script>
</head>
<body>
<form action="" method="post">
Username
<input type="text" name="username" class="input" />
Password
<input type="password" name="password" class="input" />
<div class="g-recaptcha" data-sitekey="Google Site Key"></div>
<input type="submit" value="Log In" />
<span class='msg'><?php echo $msg; ?></span>
</form>
</body>
</html>
<head>
/* Google reCaptcha JS */
<script src="https://www.google.com/recaptcha/api.js"></script>
</head>
<body>
<form action="" method="post">
Username
<input type="text" name="username" class="input" />
Password
<input type="password" name="password" class="input" />
<div class="g-recaptcha" data-sitekey="Google Site Key"></div>
<input type="submit" value="Log In" />
<span class='msg'><?php echo $msg; ?></span>
</form>
</body>
</html>
index.php
Contains PHP code, here you have to modify the Google Secret Key.
<?php
include("db.php");
session_start();
$msg='';
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$recaptcha=$_POST['g-recaptcha-response'];
if(!empty($recaptcha))
{
include("getCurlData.php");
$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;
$res=getCurlData($url);
$res= json_decode($res, true);
//reCaptcha success check
if($res['success'])
{
//Include login check code
}
else
{
$msg="Please re-enter your reCAPTCHA.";
}
}
else
{
$msg="Please re-enter your reCAPTCHA.";
}
}
?>
include("db.php");
session_start();
$msg='';
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$recaptcha=$_POST['g-recaptcha-response'];
if(!empty($recaptcha))
{
include("getCurlData.php");
$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;
$res=getCurlData($url);
$res= json_decode($res, true);
//reCaptcha success check
if($res['success'])
{
//Include login check code
}
else
{
$msg="Please re-enter your reCAPTCHA.";
}
}
else
{
$msg="Please re-enter your reCAPTCHA.";
}
}
?>
Login Check Code
This code will verify username and password details in database.
$username=mysqli_real_escape_string($db,$_POST['username']);
$password=md5(mysqli_real_escape_string($db,$_POST['password']));
if(!empty($username) && !empty($password))
{
$result=mysqli_query($db,"SELECT id FROM users WHERE username='$username' and passcode='$password'");
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
if(mysqli_num_rows($result)==1)
{
$_SESSION['login_user']=$username;
header("location: home.php"); //Success redirection page.
}
else
{
$msg="Please give valid Username or Password.";
}
}
else
{
$msg="Please give valid Username or Password.";
}
$password=md5(mysqli_real_escape_string($db,$_POST['password']));
if(!empty($username) && !empty($password))
{
$result=mysqli_query($db,"SELECT id FROM users WHERE username='$username' and passcode='$password'");
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
if(mysqli_num_rows($result)==1)
{
$_SESSION['login_user']=$username;
header("location: home.php"); //Success redirection page.
}
else
{
$msg="Please give valid Username or Password.";
}
}
else
{
$msg="Please give valid Username or Password.";
}
getCurlData.php
CURL function for Google reCaptcha verification. Enable php_curl extension in php.ini configuration file.
<?php
function getCurlData($url)
{
$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");
$curlData = curl_exec($curl);
curl_close($curl);
return $curlData;
}
?>
function getCurlData($url)
{
$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");
$curlData = curl_exec($curl);
curl_close($curl);
return $curlData;
}
?>
db.php
Database configuration file, modify username, password and database values.
<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'username');
define('DB_PASSWORD', 'password');
define('DB_DATABASE', 'database');
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?>
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'username');
define('DB_PASSWORD', 'password');
define('DB_DATABASE', 'database');
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?>
It's impressive how Google team has enhanced Recaptcha mechanism. Understanding if you are a robot or not without typing anything: incredible! I think this will bring a great improvement in conversion, since it requires fewer efforts to users. Thanks for sharing!
ReplyDeleteI love It!...
ReplyDeleteawesome :)
ReplyDeleteThis seems very interesting. Will try to add this recaptha coding into my website. Thanks for sharing!
ReplyDeleteit's helpful
ReplyDeletethanx :)
Beautiful captha system. Hope it will also be implemented to blogger comment system soon
ReplyDeleteWhy you are showing your site secret key ? Keep it secret.
ReplyDelete@Shahid That demos secret key is not working.
ReplyDeleteHello sir your ,
Deletei have a question for recaptcha ,i have implemented this on my website and user need to recaptcha themselves before going to home page,well i want to show this captcha method once in day not every time they visit my home page
This is much better than the official documentation. I would like to suggest a small fix to getCurlData.php
ReplyDeleteCurl on most servers will probably generate an error on https pages, which google requires. This is because cURL has not been configured to trust the server’s HTTPS certificate. I just added
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
to the curl settings which forces curl to accept any server certificate and voila! it works.
i'm not a robot
ReplyDeleteSrinivas, is there a way to know if the checkbox is clicked or not?
ReplyDeleteYour code takes place after the checkbox is clicked. What i need to do is to let the user know that he/she did not clicked the captcha checkbox.
Thanks
thanx master
ReplyDeleteThis is an amazing solution method. Thanks google and srinivas
ReplyDeleteMe gusta este nuevo recaptcha. Es fantástico
ReplyDeleteThis is awesome post. thanks bro i follow all time you.
ReplyDeletegood job
ReplyDeleteHow can I do the same thing via ajax
ReplyDeleteReally useful one for Developers..Thanks bro
ReplyDeleteGreat and nice feature reCaptcha. Tnats too good new v.
ReplyDeleteAwesome...I love it
ReplyDeleteThat sounds great! Thank you for sharing :)
ReplyDeletesir
ReplyDeletei want to know that which tools you use to create images for your post.
it would be really helpful...
thanx..
sir
ReplyDeletei want to know that which tools you use to create images for your post.
it would be really helpful...
thanx..
@srinivas, I think using md5() for hashing is not the good option.. we can use password_hash()
ReplyDeleteThx, really helpful and exactly what the official documentation was/is missing.
ReplyDeleteAmazing and very nice coding i love it. Thank you guys.
ReplyDeleteWhy when I try the demo, it show a captcha verification code?
ReplyDeleteThanks for sharing the information with us. It will help reducing spams.
ReplyDeleteCan you please check on firefox. Why it is not working as it works on chrome.
ReplyDeleteawesome script very help me,thanks for your share
ReplyDeletehelp me
ReplyDeleteFatal error: Call to undefined function curl_init() in C:\xampp\htdocs\Google_reCaptcha2\getCurlData.php on line 4
Seen this on few websites. This is better than older system. Hope it blocks more spammers.
ReplyDeleteMany Websites have been started following this captcha function. But I do not know that it will block some spammers or not. But we are still hoping. Thank you
ReplyDeletethank you useful information i will add this captcha to my website
ReplyDeleteit doesn't work for me and for a lot of people
ReplyDeletecuriously, though, there only seems to be positives remarks here
so much for trust, huh?
Very useful information, i am searching this kind of tutorial since last few days. thank you so much for writing this post.
ReplyDeletethanks for tutorial
ReplyDeleteAnd if only for a simple form, how would I do?
ReplyDeleteThis is more useful then crazy word which we're not able to understand. I will definatly use this codes for my blog - Thank you for sharing :)
ReplyDeletehey, i just get a blank result from google, no error nothing just a empty result what can i do?
ReplyDeleteI don't understand where to put the HTML code with the Google reCaptcha widget snippet, which will contain the site key. It is written in html. My website is php, and the index page is index.php. Do I have to include a separate html page with this code, and if so what is it named?
ReplyDeleteWhere do I insert the HTML code with Google reCaptcha widget snippet when my index page is php? Do I create a separate html page for this purpose, and if so what should it be named?
ReplyDeleteThanks for tutorial
ReplyDeleteThanks for Sharing. I have been searching it for since long time but Finally I found & succesfully made it to work.
ReplyDeletegrt wrk...luv it
ReplyDeletei tried this to my site,thanks
ReplyDeletedoes the new recaptcha offers better prevention of spam as compared to older recaptcha
ReplyDeletehi
ReplyDeleteplease help me
1-can i use google recaptcha in localhost?
2-i get secret key and data-sitekey from google but when test on localhost give me false?
You need to generate separate keys for your localhost domain. e.g. dev.example.com differs from www.example.com. I believe common keys won't work for them.
DeleteReally great! Thanks for sharing...
ReplyDeleteg8 code
ReplyDeleteGreat work.Thanks for sharing..
ReplyDeleteWorks like a charm!
ReplyDeletemultiple google recaptcha implementation on same page in php how its possible...
ReplyDeleteThanks a lot for the code. I struggle 2 days to validate the google captcha
ReplyDeleteThis info should be part of the documentation that google provides. It works fine on my php based site.
ReplyDeleteI want to skip the image verification process in recaptcha.. I need only checkbox validation.. is it possible to to?
ReplyDeleteAwesome Tutorial
ReplyDeleteThis is more useful then crazy word which we're not able to understand. I will definatly use this codes for my blog - Thank you for sharing :)
Thanx 4 this great tutorial. It's very simple and useful for web based applications and i will try to add this recaptha into my own website. thanx again for sharing.
ReplyDeleteThanks 9 you Salve My Problem Thanks. I am Happy.
ReplyDeleteVery good thank you
ReplyDeleteThanks for great work, i am wandering on blog for this.
ReplyDeletenice, thank you
ReplyDeletethis code not returning success parameter in my website, what to do..?
ReplyDeletewow nicee
ReplyDeleteThis is more useful then crazy word which we're not able to understand. I will definitely use this codes for my blog.
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDelete