How to integrate google reCaptcha in login page in PHP?

In this tutorial we will discuss how to integrate google reCaptcha in login page in PHP. reCAPTCHA is a free service that protects your site from spam and abuse. It uses advanced risk analysis techniques to tell humans and bots apart. With the new API, a significant number of your valid human users will pass the reCAPTCHA challenge without having to solve a CAPTCHA.  This protects your website for spammers and robots. First you have to register on google reCaptcha  application. click here to register.

google reCAPTCHA in php

You will see the Site key and Secret key after registration.

google reCAPTCHA in php

We will use in your login page google reCAPTCHA  here. below are the code.This is how your file structure.

db.php
getCurlData.php
index.php
 
<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', '');
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?>
<?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;
}
?>
index.php   
<?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='your secret';
$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>Google reCaptcha 2</title>
<link rel="stylesheet" href="css/style.css"/>
<script src='https://www.google.com/recaptcha/api.js'></script>	
</head>
<body>
<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=" your site key"></div>
<br/>
<input type="submit" class="button button-primary" value="Log In" id="login"/> 
 
<span class='msg'><?php echo $msg; ?></span>
</form>
</body>
</html>

When you run the code your page will look like this

google reCaptcha in php

Hope you have enjoyed this article how to integrate google reCaptcha in login page in PHP.