Recently Google has been introduced new reCaptcha API called “Are you a robot?”. They name it “NO CAPTCHA reCAPTCHA”. reCAPTCHA is a free service and It is designed to protect your website from spams and abuse. In this tutorial we are going to show you how to integrate it into your website using PHP.
Before integrating reCaptcha on website we required reCaptch key. Below are steps to get reCaptcha key
1. Click here to create a Google reCaptcha application.
2. Provide your domain detail http:// or https:// (e.g www.example.com) for website registration
3. After registration you will get 2 keys
Site key – This will be used in HTML code
Secret Key - This for communication between your site and Google.
Now we have to write the simple HTML code with Google reCaptcha:
<?php
require_once 'GooglePageRank.php';
$webUrl=$_POST['website_url'];
$objGooglePageRank = new GooglePageRank();
$result = "$webUrl has Google PageRank:". $objGooglePageRank->get_google_pagerank($webUrl);
?>
index.php:
Contains PHP code, Here we have to modifed Google Secret Key
<?php
if(isset($_POST['submit'])){
$captcha=$_POST['g-recaptcha-response'];
if(!empty($captcha))
{
$errMsg= '';
$google_url="https://www.google.com/recaptcha/api/siteverify";
$secret=_YOUR_SECRET_KEY;
$ip=$_SERVER['REMOTE_ADDR'];
$captchaurl=$google_url."?secret=".$secret."&response=".$captcha."&remoteip=".$ip;
$curl_init = curl_init();
curl_setopt($curl_init, CURLOPT_URL, $captchaurl);
curl_setopt($curl_init, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_init, CURLOPT_TIMEOUT, 10);
$results = curl_exec($curl_init);
curl_close($curl_init);
$results= json_decode($results, true);
if($results['success']){
$errMsg="Valid reCAPTCHA code. You are human.";
}else{
$errMsg="Invalid reCAPTCHA code.";
}
}else{
$errMsg="Please re-enter your reCAPTCHA.";
}
}
?>
if(isset($_POST['submit'])){
$captcha=$_POST['g-recaptcha-response'];
if(!empty($captcha))
{
$errMsg= '';
$google_url="https://www.google.com/recaptcha/api/siteverify";
$secret=_YOUR_SECRET_KEY;
$ip=$_SERVER['REMOTE_ADDR'];
$captchaurl=$google_url."?secret=".$secret."&response=".$captcha."&remoteip=".$ip;
$curl_init = curl_init();
curl_setopt($curl_init, CURLOPT_URL, $captchaurl);
curl_setopt($curl_init, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_init, CURLOPT_TIMEOUT, 10);
$results = curl_exec($curl_init);
curl_close($curl_init);
$results= json_decode($results, true);
if($results['success']){
$errMsg="Valid reCAPTCHA code. You are human.";
}else{
$errMsg="Invalid reCAPTCHA code.";
}
}else{
$errMsg="Please re-enter your reCAPTCHA.";
}
}
?>
No comments:
Post a Comment