Thursday, June 21, 2012

reCaptcha and Google Like CAPTCHA with PHP

Looking for Google style Captcha (Human Verification Code), or may be the popular reCaptcha style captcha script for PHP projects, then this post is for you. Here we have explained how to implement CAPTCHA using the cool-php-captcha script for forms. Hence use it and add security to your forms, saving fake registration from automated bots.




We have here both GOOGLE and reCaptcha style CAPTCHA script in php. Both the scripts are almost same, just the CSS styling is different.


Google Style





  index.php  

Contains HTML code for form, and the Captcha Image source - captcha.php ( File available in download script)

<form action="captcha_check.php" method="POST">
<div>The text displayed below is for security check, you need to enter it correctly .</div>

<img src="captcha.php" id="captcha" />
<a href="#" onclick="
document.getElementById('captcha').src='captcha.php?'+Math.random();
document.getElementById('captcha-form').focus();"
id="change-image">Not readable? Change text.</a>
</br;>

<div>Enter the text displayed above:</div>
<input class="input" type="text" value="" name="captcha">
<input type="submit" name="commit" value="SUBMIT"/>
</form>


  captcha_check.php  

Contains the PHP code

<?php
session_start();
if (!empty($_REQUEST['captcha'])) 
{
  if (empty($_SESSION["captcha"]) || trim(strtolower($_REQUEST['captcha'])) != $_SESSION["captcha"]) 
 {
  header("location:index.php?attempt=fail");
//Error in case wrong word is entered
 } 
else
{
  if($_SERVER["REQUEST_METHOD"] == "POST")
{
    header("location:home.php");   
// Home.php is the destination page
}
}
unset($_SESSION["captcha"]);
}
else
{
header("location:index.php?attempt=null");
//Error in case no word is entered
}
?> 



reCaptcha Style




The whole code is same for this too, just reCaptcha like CSS styling is added. Thereby making it look like reCaptcha style.

Both the scripts are almost same, with minor changes, download the one which best suits your requirements.
For ant query, do let us know through your comments.

We love hearing from you.

( We used cool-php-captcha source code for this script.)

1 comment:

  1. Bro Thank you so much for this tutorials because of you i have started to have a hope that my project will be successfully be implemented may god bless,But i have been wondering how Facebook shows user is typing notification while chatting...please i loved the way you simplify yr coding technics,if you will have time discuss that with us plz,you can as well as let mi know the update at friendin36@gmail.com

    ReplyDelete