Skip to content

Google New reCaptcha using PHP – Are you a Robot?

  • by
hero-recaptcha-demo

Today i would like to show you how to implement Google New reCaptcha using PHP. I like the new design it is clean and impressive, hope you will like it. reCaptcha 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.
hero-recaptcha-demo

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

2- Register Your Website
Give your website domain details without http: .

3- You will use Google Site Key in HTML code.
4- Google Secret Key will help your website to communication with Google.

 

google-recaptchaHTML 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" id="username" class="input" />
Password
<input type="password" name="password" id="password" class="input" />
<div class="g-recaptcha" data-sitekey="Google Site Key"></div>
<input type="submit"  value="Log In" />
<span class='msg'><?php echo $message; ?></span>
</form>
</body>
</html>

index.php
Contains PHP code, here you have to modify the Google Secret Key.

<?php
include("dbConfig.php");
session_start();
$message='';
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
{
$message="Please re-enter your reCAPTCHA.";
}

}
else
{
$message ="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($dbConfig,"SELECT * FROM users WHERE username='$username' and password='$password'");
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
if(mysqli_num_rows($result)==1)
{
$_SESSION['login_user']=$username;
header("location: welcome.php"); //Success redirection page. 
}
else
{
$message ="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;
}
?>

dbConfig.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);
?>

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face – we are here to solve your problems.

Leave a Reply

Your email address will not be published. Required fields are marked *