Do you know Captcha? Hmm.. I think captcha is used to anti spam. Usually, if you want to submit a form, you must read combination codes and write it on text area. If match, the form will submitted and vice versa. It is used to detect you are machine or not.. hah so funny right? but it’s true :p
Hehe.. no more talking, I’ll show you how to create captcha with PHP.. hah PHP again? yup, for captha I used GD Library for rendering image under PHP, so make sure you have installed this extension. To get captcha with PHP you need to create at least 3 PHP file. This is without database. I use session.
First type this code and save it to anti_spam.php.
1 2 3 4 5 6 7 8 9 | <font face="arial" size="2"> <form method='POST' action='process_anti_spam.php' enctype='multipart/form-data'> <strong>Input security code:</strong><br /> <table cellpadding='3' cellspacing='1'> <tr><td><img src='image_anti_spam.php' /></td></tr> <tr><td><input type='text' name='code'/> <input name='submit' type='submit' value='CheckCode'></td></tr> </table> </form> |
Then type this code and save it to image_anti_spam.php.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?php session_start(); function strrand($length){ $str = ''; while(strlen($str)<$length){ $random=rand(48,122); if(($random>47 && $random<58)){ $str.=chr($random);}} return $str;} $text =$_SESSION['string']=strrand(6); header('Content-type: image/png'); $im=imagecreatefrompng('bg.png');//you must create png file first for background, put it on same folder $while=imagecolorallocate($im, 255, 255, 255); $font='impact.ttf';//impact.ttf font is up to you, and you must put it on same folder $fontsize=28; imagettftext($im, $fontsize, 0, 40, 40, $white, $font, $text); imagepng($im); imagedestroy($im); ?> |
The last one type this code and save it to process_anti_spam.php.
1 2 3 4 5 6 7 8 9 | <? session_start(); echo "<font face=\"arial\" size=\"6\">"; if($_POST['code']!=$_SESSION['string']){ echo "You are Machine!!^^"; }else{ echo "Wellcome Human!!^^</font>"; } ?> |
After you create all files above. Try to run Captcha with anti_spam.php under localhost.

Read the codes and type it on text area. Finish? Submit it. If you get error message, it’s mean you failed to submit or your codes doesn’t match >.< or “You are Machine!!” haha.. but if you get welcome , congratulation you are human :p ( no just kidding about machine and human).
Nice right^^ Get PHP more powerful then.. I’ll try to make it with Ming Library. See you on next post. Hmm have any trick? Share it on comment please^^ thx.
To be continue…


One Comment Trackback URL | Comments RSS
April 27th, 2009 at 12:32 pm
Thank`s very much !
One Trackback