Sunday, August 7, 2011

Making your own php captcha generator script

Heyy everyone,

As I said yesterday in ma previous tutorial [You should read it if you dont know what i am talking about Smile] here I am with the captcha one. This would be a pretty simple, one file based approach to making a simple yet successful captcha generator.

What anti-spamming techniques used?

Mainly there are 3 :

1.Random number + Random Number = -> The user must specify the answer
2. Random word -> User must type the word
3. Random question -> Obvous one which the user should answer correctly [ex: Are you human?]

How it works?

1. The captcha generator generates an IMAGE with the question and then put up a session variable storing the value.
2. User input though an input box.
3. Using php POST, we compare the session variable data with the user input and tell whether its a bot or human.

itz coding time Smile


1) First let's write the php script which generates the captcha image. We use the simple header-content change technique, from which we can easily bring up an image from a given text.


PHP Code:
<?php//This should be the first line as in the rule book :Dsession_start();//These variables store the Question and the answer$ques "";$ans "";//This is the MAJOR array, this holds all the random things, like the question you need to ask. You can add up new ones easily$words = array(
=> array("Num" => "Num"),
=> array("Are you human?" => "yes"),
=> array("Type 'one' " => "one"),
=> array("Type 'test' " => "test"),
=> array("AxHGA" => "AxHGA"),
=> array("zontek" => "zontek"),
=> array("12terd " => "12terd")
//Now we need to pic up a random question, array_rand is the perfect thing to this
$r array_rand($words);
//Then we check about what we have in the select array
//If we have the "NUM" selected, that is a special one
        //Num means the user will be prompted to doa simple addition like 5+6
case "Num":
//Pretty basic stuff, generate 2 random numbers and tell the user to put the addition
$i rand(1,10);
$j rand(1,10);
$ans $i+$j;
$ques "$i + $j = ";
//If not a number, ask the user a question or ask him to type a word
$key key($words[$r]);
$ques $key;
$ans $words[$r][$key];
//NOW we put up the answer to the session variable$_SESSION['cap'] = strtolower($ans);//This would change the content type, or in english this would tell the browser that
//what ever retuened by this script is an image
header('Content-Type: image/png');
//Following code is to generate the image from the test
//We first specify colour ranges, you can refer to the php manaul for more
$img imagecreatetruecolor(250,30);//In the above code, the image size is set to 250x30$white imagecolorallocate($img,255,255,255);$grey imagecolorallocate($img,128,128,128);$black imagecolorallocate($img,0,0,0);//Filling the rectangle with white as we need black text on whiteimagefilledrectangle($img,0,0,399,29$white);$text $ques;//THE below code is CRITICAL. This is the palce where we tell which font to use.
//Choose any ttf you like and name it as font.ttf or change the following code, make sue
//you put the path to the file correctly [i used STENCIL so that parsers will find it hard to detect]
$font "./font.ttf";imagettftext($img,20,0,11,21,$grey,$font,$text);imagettftext($img,20,0,10,20,$black,$font,$text);//Creating a PNG image, i use png cuz i <3 png [really its so small and efficient ;)]imagepng($img);//And then remove the memory parts once the output is givenimagedestroy($img);?>

2) Haa that's all Big Grin But you still need to know how to use the thing. We make our index.php and ask the user for the input as determine whether it is a spammer or not.


PHP Code:
//This is the code for comparing the user input against the session variable
$cap $_POST['captcha'];
$_SESSION['cap']==strtolower($cap)) echo "Okay you are human :)";
        else echo 
"Off You go BOT / SPAMMER!!";
<title>Simple Captcha Script</title>
<form action="index.php" method="post">
<img src="captcha.php" align="absmiddle" />
<!-- NOTE how the captcha.php is used as an image link, that's a whole new way to think -->
<input type="text" size="20" name="captcha" />
<input type="submit" name="done" value="Login" />

3) Upload the stuff and make sure you have the following files in the same directory level :

index.php captcha.php font.ttf

4) YEppee it's done, now you have your own little cpatcha script, well this is really basic, but once you know what to do you have no limits Big Grin

Feel free to develop the code [and be kind enough to send me one as well Smile]

See the demo of the script here :

That's all for today

See ya all with some other thing (no promises today though Big Grin)


No comments:

Post a Comment