Google has announced a new service to prevent spams and attacks on your website. They name it “NO CAPTCHA reCAPTCHA” . Google reCAPTCHA is designed to protect your website from spams and abuse.
In this tutorial, I am going to show you how to integrate it into your website.
Step 1
Sign up and get your keys here: https://www.google.com/recaptcha/admin (you will get a SITE key and a SECRET key, used later)
Step 2
Include this on your page:
<script src="https://www.google.com/recaptcha/api.js"></script>
Step 3
Add the following into your form:
<script src="https://www.google.com/recaptcha/api.js"></script>
Step 4
On form submission do this:
$recaptcha = $_POST['g-recaptcha-response'];
$res = reCaptcha($recaptcha);
if(!$res['success']){
// Error
}
Using the following function:
function reCaptcha($recaptcha){
$secret = "YOUR SECRET KEY";
$ip = $_SERVER['REMOTE_ADDR'];
$postvars = array("secret"=>$secret, "response"=>$recaptcha, "remoteip"=>$ip);
$url = "https://www.google.com/recaptcha/api/siteverify";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
$data = curl_exec($ch);
curl_close($ch);
return json_decode($data, true);
}
This code is free to use at your own discretion. It comes without warranty.
Are you want to get implementation help, or modify or enhance the functionality of this script? Submit Paid Service Request Starting from Rs. 500 /-
0 Comment('s) to “How to add Google reCAPTCHA to a Form (PHP/HTML)”
Leave a Reply