3 Major Anti-spamming techniques used?
- Mathematical Operation like Random number + Random Number = -> The user must specify the answer
- Random word -> User must type the word
- Random question -> Obvious one which the user should answer correctly [ex: Are you human?]
- The captcha generator generates an IMAGE with the question and then put up a session variable storing the value.
- User input though an input box.
- Using php POST, we compare the session variable data with the user input and tell whether its a bot or human.
The Code
- 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.
captcha.php
PHP Code:<?php //This should be the firstline as in the rule book :D session_start();
//These variables store theQuestion and the answer $ques = ""; $ans = "";
//This is the MAJOR array, this holdsall the random things,like the question you need to ask.You can add up new ones easily $words = array(
0 => array("Num" => "Num"),
1 => array("Are you human?" => "yes"),
2 => array("Type 'one' " => "one"),
3 => array("Type 'test' " => "test"),
4 => array("AxHGA" => "AxHGA"),
5 => array("zontek" => "zontek"),
6 => 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
switch(key($words[$r])){
//If we have the "NUM" selected, that is a
special one
//Num means the user will be prompted to
do a simple addition like 5+6
case "Num":
//Pretty basic stuff, generate 2 random numbers andtell the user to put the addition
$i = rand(1,10);
$j = rand(1,10);
$ans = $i+$j;
$ques = "$i + $j = ";
break;
default:
//If not a number, ask the user a question or askhim to type a word
$key = key($words[$r]);
$ques = $key;
$ans = $words[$r][$key];
break;
}
//NOW we put up the answer to the session variable
$_SESSION['cap'] = strtolower($ans);
//This would change the content type, or in englishthis 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 white
imagefilledrectangle($img,0,0,399,29, $white); $text = $ques;
//THE below code is CRITICAL. This is the palcewhere we tell which font to use.
//Choose any ttf you like and name it as font.ttf orchange the following code, make sue
//you put the path to the file correctly [i used STENCILso 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 given
imagedestroy($img); ?>index.php<?php
session_start();
if(isset($_POST['done'])){
if(isset($_SESSION['cap'])){
//This is the code for comparing the user inputagainst the session variable
$cap = $_POST['captcha'];
if($_SESSION['cap']==strtolower($cap)) echo "Okay you are human :)";
else echo "Off You go BOT / SPAMMER!!";
}
} ?> <html>
<head>
<title>Simple Captcha Script</title>
</head>
<body>
<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" /><br />
<input type="submit" name="done" value="Login" />
</form>
</body>
</html>Upload the stuff and make sure you have the following files in the same directory level : index.php captcha.php font.ttf
No comments:
Post a Comment