php form

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

Three files are required for the captcha form.  (CAP3)
1)    formc.html     File size: 625 bites    
This creates html form for submission of values with refresh image.
2)    capc.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 (formc.html)
<html> <head> </head> <body> <form action="http://www.xxx.com/recheck.php" 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="220" name="comments"></textarea><br /> Enter captcha code: <br /> &nbsp;&nbsp;&nbsp;<img alt="" src="http://www.xxx.com/php/capc.php" /> <br /> <input type="text" size="3" name="cap_inp" /> &nbsp;&nbsp; <input type="submit" value="Enter" name="submit" /> </form> </body> </html>

CAPTCHA GENERATION (capc.php)
<!--php //Start the session so we can store what the security code actually is session_start(); //Send a generated image to the browser create_image(); exit(); function create_image() { //Let's generate a totally random string using md5 $md5_hash = md5(rand(0, 999)); //We don't need a 32 character long string so we trim it down to 4 $security_code = substr($md5_hash, 15, 4); //Set the session to store the security code //$_SESSION["security_code"] = $security_code; $_SESSION["cap_ans"] = $security_code; //Set the image width and height $width = 100; $height = 25; //Create the image resource $image = ImageCreate($width, $height); //We are making three colors black and gray $black = ImageColorAllocate($image, 0, 0, 0); $grey = ImageColorAllocate($image, 204, 204, 204); //Make the background black ImageFill($image, 0, 0, $grey); //Add randomly generated string in black to the image ImageString($image, 5, 30, 5, $security_code, $black); //Tell the browser what kind of file is come in header("Content-Type: image/jpeg"); //Output the newly created image in jpeg format ImageJpeg($image); //Free up resources ImageDestroy($image); } ?-->

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