Google New reCaptcha using PHP - Are you a Robot?
Wall Script
Follow Me:
Wednesday, December 03, 2014

Google New reCaptcha using PHP - Are you a Robot?

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.

Google New reCaptcha using PHP - Are you a Robot?


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 New reCaptcha using PHP - Are you a Robot?

Google Site Key
You will use this in HTML code.
Google New reCaptcha using PHP - Are you a Robot?

Google Secret Key
This will help your website to communication with Google.
Google New reCaptcha using PHP - Are you a Robot?


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>

Google New reCaptcha using PHP - Are you a Robot?


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.";
}

}
?>

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.";
}

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;
}
?>


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);
?>
Was this article helpful?
Thanks! Your feedback helps us to improve 9lessons.info


56 comments:

  1. 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!

    ReplyDelete
  2. This seems very interesting. Will try to add this recaptha coding into my website. Thanks for sharing!

    ReplyDelete
  3. Beautiful captha system. Hope it will also be implemented to blogger comment system soon

    ReplyDelete
  4. Why you are showing your site secret key ? Keep it secret.

    ReplyDelete
  5. @Shahid That demos secret key is not working.

    ReplyDelete
  6. This is much better than the official documentation. I would like to suggest a small fix to getCurlData.php

    Curl 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.

    ReplyDelete
  7. Srinivas, is there a way to know if the checkbox is clicked or not?

    Your 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

    ReplyDelete
  8. This is an amazing solution method. Thanks google and srinivas

    ReplyDelete
  9. Me gusta este nuevo recaptcha. Es fantástico

    ReplyDelete
  10. This is awesome post. thanks bro i follow all time you.

    ReplyDelete
  11. How can I do the same thing via ajax

    ReplyDelete
  12. Really useful one for Developers..Thanks bro

    ReplyDelete
  13. Great and nice feature reCaptcha. Tnats too good new v.

    ReplyDelete
  14. That sounds great! Thank you for sharing :)

    ReplyDelete
  15. sir
    i want to know that which tools you use to create images for your post.
    it would be really helpful...
    thanx..

    ReplyDelete
  16. sir
    i want to know that which tools you use to create images for your post.
    it would be really helpful...
    thanx..

    ReplyDelete
  17. @srinivas, I think using md5() for hashing is not the good option.. we can use password_hash()

    ReplyDelete
  18. Thx, really helpful and exactly what the official documentation was/is missing.

    ReplyDelete
  19. Amazing and very nice coding i love it. Thank you guys.

    ReplyDelete
  20. Why when I try the demo, it show a captcha verification code?

    ReplyDelete
  21. Thanks for sharing the information with us. It will help reducing spams.

    ReplyDelete
  22. Can you please check on firefox. Why it is not working as it works on chrome.

    ReplyDelete
  23. awesome script very help me,thanks for your share

    ReplyDelete
  24. help me

    Fatal error: Call to undefined function curl_init() in C:\xampp\htdocs\Google_reCaptcha2\getCurlData.php on line 4

    ReplyDelete
  25. Seen this on few websites. This is better than older system. Hope it blocks more spammers.

    ReplyDelete
  26. Many 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

    ReplyDelete
  27. thank you useful information i will add this captcha to my website

    ReplyDelete
  28. it doesn't work for me and for a lot of people
    curiously, though, there only seems to be positives remarks here
    so much for trust, huh?

    ReplyDelete
  29. Very useful information, i am searching this kind of tutorial since last few days. thank you so much for writing this post.

    ReplyDelete
  30. And if only for a simple form, how would I do?

    ReplyDelete
  31. This 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 :)

    ReplyDelete
  32. hey, i just get a blank result from google, no error nothing just a empty result what can i do?

    ReplyDelete
  33. I 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?

    ReplyDelete
  34. Where 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?

    ReplyDelete
  35. Thanks for Sharing. I have been searching it for since long time but Finally I found & succesfully made it to work.

    ReplyDelete
  36. does the new recaptcha offers better prevention of spam as compared to older recaptcha

    ReplyDelete
  37. hi

    please 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?

    ReplyDelete
  38. Really great! Thanks for sharing...

    ReplyDelete
  39. multiple google recaptcha implementation on same page in php how its possible...

    ReplyDelete
  40. Thanks a lot for the code. I struggle 2 days to validate the google captcha

    ReplyDelete
  41. This info should be part of the documentation that google provides. It works fine on my php based site.

    ReplyDelete

Make in India