SIMPLE MATH CAPTCHA FORMS

For avoiding automatic form submissions, one has to consider some kind of verification. One of the best ways is to use an math captcha instead of image captcha. What it does is to dynamically create an math formula with a random string displayed on it. Then visitor is asked to type that string in a text field and once the form is submitted it checks if the string on the math value matches the one inputted by the user. Because there is no easy way to read a text from the math value, this is a good way to protect your web forms from spammers.

Form no: CAP1

Two files are required for the captcha form.  (CAP1)
1)    captcha.php     File size: 625 bites    
This creates form as well as captcha math function.

2)     recheck.php     File size: 627 bites   
This validates the submitted captcha value with the captcha math function.

Both the files files put together 1.25 kb very simple and handy form. The source codes are given below. If you want to add few more inputs, design in both the files accordingly.

SUBMIT FORM AND CAPTCHA (MATH) GENERATION (in php)
<!--php session_start(); echo '<form action="recheck.php" method="post"--> Enter Name: <br> <input type="text" name="name" maxlength="30"><br> Enter Place: <br> <input type="text" name="place"maxlength="30"><br> Comments please: <br> <textarea name="comments" maxlength="220" cols="17" rows="3"></textarea><br>'; $rand_int1 = substr(mt_rand(),0,1); $rand_int2 = substr(mt_rand(),0,1); $cap_ans = $rand_int1 + $rand_int2; $_SESSION['cap_ans'] = $cap_ans; echo 'Enter the math value: <br> '.$rand_int1.' + '.$rand_int2.' = ? <input type="text" name="cap_inp" size="1"> <input type="submit" value="SUBMIT"> </form>'; ?>
CAPTCHA VALIDATION (recheck.php):
<!--php session_start(); $cap_inp = $_POST['cap_inp']; $name = $_POST['name']; $place = $_POST['place']; $comments = $_POST['comments']; $cap_ans = $_SESSION['cap_ans']; if($cap_inp != $cap_ans) { echo 'Captcha is wrong! // You may direct to some page, as you wish <br--><br> Sorry Mr. '.$name.' ,<br> Please enter data correctly. <br><br><br> <a href="http://www.freemenu.info/p/list.html"> CLICK HERE</a> '; } else { echo 'Captcha is correct! // You may design what ever you want <br><br> Thank you Mr. '.$name.' ,<br> You are staying at:'.$place.'<br> Message: '.$comments.' <br><br><br> Get the full source code here:<br> <a href="http://www.freemenu.info/p/list.html">www.freemenu.info</a>'; } ?>

No comments:

Click Here To add Comment

Post a Comment