Google reCAPTCHA tutorial
Rcently Google has announced new service to prevent spams to your website. They name it “NO CAPTCHA reCAPTCHA” .
In this tutorial i am going to show you how to use it in your website. this is simple script. .
In this tutorial i am going to show you how to use it in your website. this is simple script. .
1.Register your website and get Secret Key.
Very first thing you need to do is register your website on Google recaptcha to do that click here.
You have To Login to your Google account and submit the form.
ps : in my case i add the localhost.dev domain by modifing the host file in C:\Windows\System32\Drivers\etc
add the line
127.0.0.1 localhost.dev
<VirtualHost *:80>
DocumentRoot /path/to/document/root/site/
ServerName site.dev
ServerAlias localhost.dev
# Other directives here
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /path/to/document/root/
ServerName localhost
# Other directives here
</VirtualHost>
Once submit, Google will provide you following two information.
- Site key
- Secret key
2.Integrate it
To integrate it you need to put it in client side as well as in Server side. In client HTML page you need to integrate this line before <HEAD> tag.
<script src='https://www.google.com/recaptcha/api.js'></script>
And to show the widget into your form you need to put this below contact form, comment form etc.
When the form get submit to Server, this script will send ‘g-recaptcha-response’ as a POST data. You need to verify it in order to see whether user has checked the Captcha or not.
Simple comment form with Google reCAPTCH:
Index.html
<html>
<head>
<title>Google recapcha demo</title>
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body>
<h1>Google reCAPTHA Demo</h1>
<form id="comment_form" action="form.php" method="post">
<input type="email" placeholder="Type your email" size="40"><br><br>
<textarea name="comment" rows="8" cols="39"></textarea><br><br>
<input type="submit" name="submit" value="Post comment"><br><br>
<div class="g-recaptcha" data-sitekey="=== Your site key ==="></div>
</form>
</body>
</html>
<head>
<title>Google recapcha demo</title>
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body>
<h1>Google reCAPTHA Demo</h1>
<form id="comment_form" action="form.php" method="post">
<input type="email" placeholder="Type your email" size="40"><br><br>
<textarea name="comment" rows="8" cols="39"></textarea><br><br>
<input type="submit" name="submit" value="Post comment"><br><br>
<div class="g-recaptcha" data-sitekey="=== Your site key ==="></div>
</form>
</body>
</html>
This will generate this form.
On server side i am using PHP for now. So on Form submit request we will check the POST variable.
form.php
<?php
$email;$comment;$captcha;
if(isset($_POST['email'])){
$email=$_POST['email'];
}if(isset($_POST['comment'])){
$email=$_POST['comment'];
}if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
echo '<h2>Please check the the captcha form.</h2>';
exit;
}
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=YOUR SECRET KEY&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
if($response.success==false)
{
echo '<h2>You are spammer ! Get the @$%K out</h2>';
}else
{
echo '<h2>Thanks for posting comment.</h2>';
}
?>
$email;$comment;$captcha;
if(isset($_POST['email'])){
$email=$_POST['email'];
}if(isset($_POST['comment'])){
$email=$_POST['comment'];
}if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
echo '<h2>Please check the the captcha form.</h2>';
exit;
}
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=YOUR SECRET KEY&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
if($response.success==false)
{
echo '<h2>You are spammer ! Get the @$%K out</h2>';
}else
{
echo '<h2>Thanks for posting comment.</h2>';
}
?>
you can also use cURL instead of file_get_contents
This website is remarkable information and facts it's really excellent Anti captcha key
ReplyDeleteanti captchaThanks for share this wonderful information with us.
ReplyDelete