Showing posts with label php spam checker. Show all posts
Showing posts with label php spam checker. Show all posts

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>