Tag

Google reCAPTCHA v2

Browsing

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

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

<?php
include("db.php");                     // Connect Datasae
session_start();
$msg='';
if($_SERVER["REQUEST_METHOD"] == "POST")            // check the Submit method
{
$recaptcha=$_POST['g-recaptcha-response'];           // Get recapthca Value 
if(!empty($recaptcha))
{
include("getCurlData.php");                               // This is recaptcha library function
$google_url="https://www.google.com/recaptcha/api/siteverify";       // Google rechatpcha Api
$secret='6LekowITAAAAAOyP9-ZRtu8_-pOzOJ2sugK4T696';                  // Get Secret code 
$ip=$_SERVER['REMOTE_ADDR'];
$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'
{

$username=mysql_real_escape_string($db,$_POST['username']); 
$password=md5(mysql_real_escape_string($db,$_POST['password'])); 
if(!empty($username) && !empty($password))
{
$result=mysql_query($db,"SELECT id FROM raja WHERE uname='$username' and pwd='$password'");
$row=mysql_fetch_array($result);
if(mysql_num_rows($result)==1)
{
$_SESSION['login_user']=$username;
$_SESSION['password']=$password;
header("location: home.php");
}
else
{
$msg="Please give valid Username or Password.";
}

}
else
{
$msg="Please give valid Username or Password.";
}
/********/
}
else
{
$msg="Please re-enter your reCAPTCHA.";
$msg="Please";
}
}
else
{
$msg="Please re-enter your something.";
}
}
?>

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Google reCaptcha 2</title>
<link rel="stylesheet" href="css/style.css"/>
<script src='https://www.google.com/recaptcha/api.js'></script>      <!-- Recapthca Js function -->
</head>
<body>
<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" />
<br/><br/>
<div class="g-recaptcha" data-sitekey="6LekowITAAAAAE99rGgQgE3bpnP2UVbYLZoe0oNw"></div>              <!-- rechapthca input type -->
<br/>
<input type="submit" class="button button-primary" value="Log In" id="login"/> 
<span class='msg'><?php echo $msg; ?></span>
</form>
</div>
</div>
</body>
</html>

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 – http://www.google.com/recaptcha/intro/index.html.
Login with you google account.
Register a new site ->
Label name – Write something ->
Domain name – www.yourwebsite.com ->
owners – yourmail@gamil.com

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="https://www.google.com/recaptcha/api/siteverify";       // Google rechatpcha Api
$secret='6LekowITAAAAAOyP9-ZRtu8_-pOzOJ2sugKcvcxvc';                  // Replace Your Secret key Here 
$ip=$_SERVER['REMOTE_ADDR'];
<div class="g-recaptcha" data-sitekey="6LekowITAAAAAE99rGgQgE3bpnP2UVbYLZoe0oNw"></div>  <!-- Replace your site key here -->

Step 4: getCurlData.php code

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

Step 6: Add Css Style

body
{
font-family: "Helvetica",Arial,sans-serif;
font-weight: 500;
color:#333;
background-color:#fbfbfb;
}
label
{
	width:100px;
	display:block;
	font-size:13px;
	color:#666666;
}
#main
{
	margin:0 auto;
	width:800px;
	text-align:center;
}
#box
{
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;
}
.input
{
	
	padding:10px;
	font-size:14px;
	border:1px solid #999999;
	width:278px;
	margin-bottom:10px;
	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;
}
.msg
{
	font-size:11px;
	color:#cc0000;
	padding:10px;
}

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