CAPTCHA SCRIPT (5 digit numeric value)

For avoiding automatic form submissions, one has to consider some kind of verification. One of the best ways is to use a five digit numeric captcha instead of image captcha. What it does is to dynamically create a image 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 image number matches the figure inputted by the user. Because there is no easy way to read a text from the image, this is a good way to protect your web forms from spammers.

Form no: CAP6

Three files are required for the captcha form.  (CAP6)
1)    formg.html     File size: 625 bites    
This creates html form for submission of values with refresh image.
2)    capg.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 (formg.html)
<html> <head> </head> <body> <form action="" method="post" name="[object HTMLInputElement]"> 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="" /> &nbsp;&nbsp;<a href="javascript:reloadImg()"> <img src="" /></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=""+d.getTime(); } </script> </form> </body> </html>

<!--PHP // initialise image with dimensions of 120 x 30 pixels $image = @imagecreatetruecolor(120, 30) or die("Cannot Initialize new GD image stream"); // set background to white and allocate drawing colours $background = imagecolorallocate($image, 0xFF, 0xFF, 0xFF); imagefill($image, 0, 0, $background); $linecolor = imagecolorallocate($image, 0xCC, 0xCC, 0xCC); $textcolor = imagecolorallocate($image, 0x33, 0x33, 0x33); // draw random lines on canvas for($i=0; $i < 6; $i++) { imagesetthickness($image, rand(1,3)); imageline($image, 0, rand(0,30), 120, rand(0,30), $linecolor); } session_start(); // add random digits to canvas $digit = ''; for($x = 15; $x <= 95; $x += 20) { $digit .= ($num = rand(0, 9)); imagechar($image, rand(3, 5), $x, rand(2, 14), $num, $textcolor); } // record digits in session variable $_SESSION['cap_ans'] = $digit; // display image and clean up header('Content-type: image/png'); imagepng($image); imagedestroy($image); ?-->

<!--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://"></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://"></a>'; } ?>

No comments:

Click Here To add Comment

Post a Comment