04 March 2011

How to make your own captcha using php

Forms are very common in web based applications. They can be used for signup, signin, posting and etc but a simple macro or a bot can spam on the site using these forms. So the easiest way to stop spam is to introduce a captcha in the web form. There are many open source captcha scripts available on the internet for free but sometimes these capctha's start annoying the user. So I wrote a very simple and user friendly captcha script which I will be showing you guys in this article.

My captcha script can be implemented in two steps:

Step1: Code for HTML Form
In the first step you need to enter a piece of code in your html form, in order to make the implementation of this captcha script easier I am using a dummy HTML form. The code is as follows:
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
  <table width="100%" border="0">
    <tr>
      <td>Select Name:</td>
          <td><label>
          <input type="text" name="test1" id="test1" />
          </label></td>
        </tr>
       <tr>
      <td>Select Email:</td>
          <td><label>
          <input type="text" name="test2" id="test2" />
          </label></td>
        </tr>
        <tr>
          <td colspan="2">Spam Check <?php
        $num1=rand(1,9); //Generate First number between 1 and 9
        $num2=rand(1,9); //Generate Second number between 1 and 9
        $captcha_total=$num1+$num2;
        echo $num1." + ".$num2;
        ?>=
            <label>
              <input name="captcha_entered" type="text" id="captcha_entered" size="5" maxlength="2" />
          </label><input type="hidden" name="captcha_total" id="captcha_total" value="<?php echo $captcha_total; ?>" /></td>
    </tr>
        <tr>
          <td colspan="2"><label>
            <input type="submit" name="button" id="button" value="submit" />
          </label></td>
        </tr>
  </table>
</form>
Step 2: Verifying the entered captcha
In the second step you need to verify the information entered in the captcha. You can easily understand the verification process from the following code:
<?php
// Checking if the form is being submitted
if ($_REQUEST['button']=="submit")
{
    //Checking if the captch entered is correct
    if ($_REQUEST['captcha_entered']==$_REQUEST['captcha_total'])
    {
        //Place your code for processing the form here
        echo "test";
    }
    else 
    {
        //Place here the error message to be displayed
        echo "<script>alert('Spam Check Failed');</script>";
    }
}
?>
Well guys thats all you need to do to create your own captcha. You can find the complete captcha script below:
<?php
// Checking if the form is being submitted
if ($_REQUEST['button']=="submit")
{
    //Checking if the captch entered is correct
    if ($_REQUEST['captcha_entered']==$_REQUEST['captcha_total'])
    {
        //Place your code for processing the form here
        echo "test";
    }
    else 
    {
        //Place here the error message to be displayed
        echo "<script>alert('Spam Check Failed');</script>";
    }
}
?>
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
  <table width="100%" border="0">
    <tr>
      <td>Select Name:</td>
          <td><label>
          <input type="text" name="test1" id="test1" />
          </label></td>
        </tr>
       <tr>
      <td>Select Email:</td>
          <td><label>
          <input type="text" name="test2" id="test2" />
          </label></td>
        </tr>
        <tr>
          <td colspan="2">Spam Check <?php
        $num1=rand(1,9); //Generate First number between 1 and 9
        $num2=rand(1,9); //Generate Second number between 1 and 9
        $captcha_total=$num1+$num2;
        echo $num1." + ".$num2;
        ?>=
            <label>
              <input name="captcha_entered" type="text" id="captcha_entered" size="5" maxlength="2" />
          </label><input type="hidden" name="captcha_total" id="captcha_total" value="<?php echo $captcha_total; ?>" /></td>
    </tr>
        <tr>
          <td colspan="2"><label>
            <input type="submit" name="button" id="button" value="submit" />
          </label></td>
        </tr>
  </table>
</form>
If you have any queries or suggestions please feel free to comment.

No comments: