How To Integrate Google ReCaptcha In Your PHP Form To Protect From Spammers

0
Shares
How To Integrate Google ReCaptcha In Your PHP Form To Protect From Spammers
Rate this post

 

Does this look familiar?

google-recaptcha-example-demo

Of course it does…

Till now, you have probably filled out dozens of forms on different websites where you had to verify that ‘I’m not a robot’.

Moreover, you already have the idea of why such captcha are used in website forms. They are the best way to protect from spammers. They generally help websites to distinguish between a human and a machine.

In the old days, it used to come with an image that had some text written. And, the user need to enter the exact text written on the image in the given input field to verify to validate on the server. Now, this is a good way to verify the human, but it is bit frustrating experience. The text written on images are little difficult to read, which turned out to be reason for decrease in getting user information.

recaptcha

To overcome this frustrating experience, Google has introduced its new Google ReCaptcha. However, before proceeding further, let’s explore what is Google ReCaptcha and its little history.

What is Google ReCaptcha?

Google ReCaptcha is a free service provided by Google for the spam/bot protection. It provides a very advanced CAPTCHA to protect websites from abusive spammers. Further, the Google allows to integrate Google ReCaptcha API into any specific page of your website so that no autobot can do spamming.

Since old Captcha was bit frustrating as it required users to read characters and type exact in the input box, Google came up with a new Captcha where users does not need to input anything in the input box.

Now that you’ve a clear understanding of Google ReCaptcha, we’ll demonstrate how to integrate Google ReCaptcha in your PHP website to protect from autobots and spammers In this PHP form tutorial.

Let’s Get Started

Create a file with Index.php for the output view.

Index.php

<?php
include("connection.php");
session_start();

$msg='';
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$recaptcha=$_POST['g-recaptcha-response'];
if(!empty($recaptcha))
{
include("getinformation.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);
if($res['success'])
{

/********/
$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");
}
else
{
$msg="Please give valid Username or Password.";
}

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

}
else
{
$msg="Please re-enter your reCAPTCHA.";
}

}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Space-O </title>
<link rel="stylesheet" href="css/style.css"/>
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body>
<div id="main">
<h1></h1>
<div id="box">
<form action="" 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="Google Site Key"></div>
<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>

Connection.php

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

Getinformation.php

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

That’s It!

Once you follow these steps, you can successfully add recaptcha to PHP form of your website. Although, it’s also important that you add other validations as well. For this, you could either contact an expert or hire PHP developer to ensure your website forms have all the necessary validations.

This page was last edited on July 23rd, 2018, at 3:26.
 
0
Shares
 

Would You Like to Develop Your Business Website? We Can Help!

Get your free consultation now