captcha

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: CAP4

Enter Name:

Enter Place:

Comments please:

  
  
Three files are required for the captcha form.  (CAP4)
1)    formh.html     File size: 625 bites    
This creates html form for submission of values with refresh image.
2)    caph.php     File size: 625 bites    
the image is five digit numeric figure. This creates captcha image and appear in the html form.
3)     recheck.php     File size: 627 bites   
This validates the submitted captcha value with the captcha math function.

The source codes are given below. If you want to add few more inputs, design in both the files accordingly.


SUBMIT FORM (formh.html)
<html> <head> </head> <body> <form action="http://www.xxx.com/recheck.php" method="post" name=" "> Enter Name: <br /> <input type="text" maxlength="30" name="name" /><br /> Enter Place: <br /> <input type="text" maxlength="30" name="place" /><br /> Comments please: <br /> <textarea rows="3" cols="17" maxlength="100" name="comments"></textarea><br /> <img id="captcha" src="http://www.xxx.com/caph.php" /> &nbsp;&nbsp; <a href="javascript:reloadImg()"> <img src="http://www.piperate.com/main/load.jpg" /></a><br /> <input type="text" size="4" name="cap_inp" /> &nbsp;&nbsp; <input type="submit" value="SUBMIT" name="submit" /> <script type="text/javascript"> function reloadImg() { var d=new Date(); document.getElementById("captcha").src="http://www.xxx.com/caph.php?a="+d.getTime(); } </script> </form> </body> </html>

CAPTCHA GENERATION (caph.php)
<!--php session_start(); $width = 120; $height = 40; $length = 4; $baseList = '0123456789abcdfghjkmnpqrstvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $code = ""; $counter = 0; $image = @imagecreate($width, $height) or die('Cannot initialize GD!'); for( $i=0; $i<10; $i++ ) { imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), imagecolorallocate($image, mt_rand(150,255), mt_rand(150,255), mt_rand(150,255))); } for( $i=0, $x=0; $i<$length; $i++ ) { $actChar = substr($baseList, rand(0, strlen($baseList)-1), 1); $x += 10 + mt_rand(0,10); imagechar($image, mt_rand(3,5), $x, mt_rand(5,20), $actChar, imagecolorallocate($image, mt_rand(0,155), mt_rand(0,155), mt_rand(0,155))); $code .= strtolower($actChar); } header('Content-Type: image/jpeg'); imagejpeg($image); imagedestroy($image); $_SESSION['cap_ans'] = $code; ?-->

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://http://www.freemenu.info/2014/08/php.html"> www.freemenu.info</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://http://www.freemenu.info/2014/08/php.html">www.freemenu.info</a>'; } ?>

No comments:

Click Here To add Comment

Post a Comment