captcha code

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

Enter Name:

Enter Place:

Comments please:

Enter captcha code:
Three files are required for the captcha form.  (CAP5)
1)    formf.html     File size: 625 bites    
This creates html form for submission of values with refresh image.
2)    capf.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 (formf.html)
<html> <head> </head> <body> <form action="" 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> Enter captcha code: <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 session_start(); //random string generator function function random($length){ //create a or array string called chars $chars ="abcdefghijklmnopqrstuvwxyz23456789"; $str = ""; //is the variable which is equal to the length of the string called chars $size = strlen($chars); for($i=0; $i<$length; $i++){ $str .= $chars[rand(0, $size-1)]; } return $str; } //the gd image adaptor in xampp $cap = random(7); $_SESSION['cap_ans'] = $cap; $image = imagecreate(100, 20); $background = imagecolorallocate($image, 0, 0, 0); $foreground = imagecolorallocate($image, 255, 255, 255); //this is going to write our string in the image imagestring($image, 5,5,1,$cap,$foreground); header("Content-type: image/jpeg"); imagejpeg($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