PHP CAPTCHA NUMBER

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

Three files are required for the captcha form.  (CAP2)
1)    forma.html     File size: 625 bites    
This creates html form for submission of values with refresh image.
2)    capa.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 (forma.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/capa.php" /> <br /> <input type="text" size="3" name="cap_inp" /> &nbsp;&nbsp; <input type="submit" value="Enter" name="submit" /> </form> </body> </html>

CAPTCHA GENERATION (capa.php)
<!--php session_start(); $code=rand(1000,9999); $_SESSION["cap_ans"]=$code; //generate image $im = imagecreatetruecolor(50, 24); //colors $bg = imagecolorallocate($im, 22, 86, 165); //background color blue $fg = imagecolorallocate($im, 255, 255, 255);//text color white imagefill($im, 0, 0, $bg); //draw text: imagestring($im, 5, 5, 5, $code, $fg); // prevent client side caching header("Expires: Wed, 1 Jan 1997 00:00:00 GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); //send image to browser header('Content-type: image/png'); imagepng($im); imagedestroy($im); ?-->

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