Google reCAPTCHA v2 (No CAPTCHA) Code For login to avoid Bots

Google+ Pinterest LinkedIn Tumblr

Google reCAPTCHA v2 (No CAPTCHA) Code For login to avoid Bots

Hi Developers, Here We go learn for Integrate Google reCAPTCHA v2 to our login form for avoid bots. reCAPTCHA protects the websites you love from spam and abuse. Google prompted users to confirm they aren’t robots by asking them to read distorted text and type it into a box, like old captcha verification Google old recapthca

But, Google figured out easier way ask our users whether or not they are robots—so, It begun rolling out a new API that radically simplifies the reCAPTCHA experience. It is Called the “No CAPTCHA reCAPTCHA” and this is how it looks:
Google new recapthca

So Here We Go the Code for Google reCAPTCHA v2,

Step 1 : Create Login page with recaptcha and session details

include("db.php");                     // Connect Datasae
if($_SERVER["REQUEST_METHOD"] == "POST")            // check the Submit method
$recaptcha=$_POST['g-recaptcha-response'];           // Get recapthca Value 
include("getCurlData.php");                               // This is recaptcha library function
$google_url="";       // Google rechatpcha Api
$secret='6LekowITAAAAAOyP9-ZRtu8_-pOzOJ2sugK4T696';                  // Get Secret code 
$url=$google_url."?secret=".$secret."&response=".$recaptcha."&remoteip=".$ip;     // Check Secret Code, Recaptcha response and Ip address
$res=getCurlData($url);           // Result

if($res['success'])                 // IF success execute this  'login process'

if(!empty($username) && !empty($password))
$result=mysql_query($db,"SELECT id FROM raja WHERE uname='$username' and pwd='$password'");
header("location: home.php");
$msg="Please give valid Username or Password.";

$msg="Please give valid Username or Password.";
$msg="Please re-enter your reCAPTCHA.";
$msg="Please re-enter your something.";

<!doctype html>
<html lang="en">
<meta charset="UTF-8" />
<title>Google reCaptcha 2</title>
<link rel="stylesheet" href="css/style.css"/>
<script src=''></script>      <!-- Recapthca Js function -->
<div id="main">
<h1>Google reCaptcha 2</h1>
<div id="box">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<label>Username</label> <input type="text" name="username" class="input" />
<label>Password </label><input type="password" name="password" class="input" />
<div class="g-recaptcha" data-sitekey="6LekowITAAAAAE99rGgQgE3bpnP2UVbYLZoe0oNw"></div>              <!-- rechapthca input type -->
<input type="submit" class="button button-primary" value="Log In" id="login"/> 
<span class='msg'><?php echo $msg; ?></span>

Step 2: Register With Google Get Your website Secret Key, Each Websites have single Key you cant use one secret key to another website.
GetreCAPTCHA registration link –
Login with you google account.
Register a new site ->
Label name – Write something ->
Domain name – ->
owners –

Step 3: After Register you will Get
You will get Your site keys and secret keys Replace it
Google new recapthca Replace

include("getCurlData.php");                               // This is recaptcha library function
$google_url="";       // Google rechatpcha Api
$secret='6LekowITAAAAAOyP9-ZRtu8_-pOzOJ2sugKcvcxvc';                  // Replace Your Secret key Here 
<div class="g-recaptcha" data-sitekey="6LekowITAAAAAE99rGgQgE3bpnP2UVbYLZoe0oNw"></div>  <!-- Replace your site key here -->

Step 4: getCurlData.php code

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: Gecko/20110319 Firefox/3.6.16");
		$curlData = curl_exec($curl);
		return $curlData;

Step 6: Add Css Style

font-family: "Helvetica",Arial,sans-serif;
font-weight: 500;
	margin:0 auto;
width: 304px;
margin: 0px auto;
text-align: left;
padding: 20px;
background-color: #ffffff;
color: #333;
border: 1px solid #e5e5e5;
box-shadow: rgba(200,200,200,0.7) 0 4px 10px -1px;
	border:1px solid #999999;
	border: 1px solid #e5e5e5;
background: #fbfbfb;
box-shadow: inset 1px 1px 2px rgba(200,200,200,0.2);

.button {
font-weight: bold;
padding: 12px 15px;
background: #3f8abf;
color: #fff !important;
font-size: 14px;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
cursor: pointer;
text-decoration: none;
text-shadow: 0 1px 0px rgba(0,0,0,0.15);
border-width: 1px 1px 3px !important;
border-style: solid;
border-color: #326e99;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
display: -moz-inline-stack;
display: inline-block;
vertical-align: middle;
zoom: 1;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
-ms-border-radius: 5px;
-o-border-radius: 5px;
border-radius: 5px;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
-webkit-box-shadow: 0 -1px 0 rgba(255,255,255,0.1) inset;
-moz-box-shadow: 0 -1px 0 rgba(255,255,255,0.1) inset;
box-shadow: 0 -1px 0 rgba(255,255,255,0.1) inset;
.button-primary {
background-color: #5fcf80 !important;
border-color: #3ac162 !important;

That’s it Enjoy the Code, Please Subscribe Us.

I'm Rajasekar - Web developer, Freelancer, Blogger and Owner of DeveloperDesks. From India lives in Bahrain. I love to do coding, Creating websites and trying different with code and designs. You Can Hire Me

Write A Comment