Today i created a new - even better -
৺captcha version for php.
The behavior is same as known from the old version - but now there are additional features:
- generic image size
- generic backgrounds (randomly selected, random Area if "bigger" thtn ৺captcha)
- generic fonts (real TFFs, randomly selected)
- generic chars (to prevent 0 o O - issues)
- generic font size
The usage of the
৺captcha is quit simple in usage:
just include it as an Image does all the magic. If you like to , you can simple use Javascript!
to create new images on demand.
The generated
৺Captcha-Code is saved into the users current seession. so you just need to compare
the build hash of passed value, with the one stored in the session - and you got it ;-)
Here some examples of created images. (remember the generic backgrounds and fonts):

no title available

no title available

no title available

no title available

no title available

no title available

no title available
Here's the Source Code of the Project. you simple need to copy it to a file, called "captcha.php",
and create a directory called "captcha", where you put in your bgs and Tiffs.
In this Version, the script will use all PNGs and TTFs in that folder automatically.
The Capture is saved to
With defined Salt as Pre- and Suffix.
<?php
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); header('Last-Modified: ' . gmdate('D, d M Y H:i:s') .' GMT'); header('Cache-Control: no-store, no-cache, max-age=0, must-revalidate');
//generate a simple captcha.
//i=l=I
//0=o=O
unset($_SESSION["Captcha"]);
/*
* Start Editing here
*/
//get Backgrounds in PNG Format
//And Fonts in TIFF format
$BgArray[] = "captcha/".$file;
$FontArray[] = "captcha/".$file;
}
//possible Chars
$CharStr = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
$FontSize = 26;
//Picture Attributes
$Width = 200;
$Height = 60;
//PAdding - distance from borders
$Padding = 10;
//Just some random chars, to create the salt for hashing.
$Salt = "23lfb9$5%§0pr(),f.";
/*
* Stop Editing here, except you know what you do
*/
//Start of creation
//5 chars per captcha
For ($i=0;$i<5;$i++){
//Create captcha
}
//Create Image
//Create a Image From possbile background
//get the size of current bg image
//Now calc the max. x and max y-offset.
$MaxXOffset = $Size[0] - $Width;
$MaxYOffset = $Size[1] - $Height;
//Now create the Chars
for ($i=0; $i<5;$i++){
//Current Char
$CurChar = $cap[$i];
//Get Random Font
$CurFontSize = $FontSize;
$CurAngleDirection = $neg[mt_rand(0
,1
)]; $CurAngle = mt_rand(0
,20
) * $CurAngleDirection;
//CurX
$WidthPerChar = ($Width - 2 * $Padding ) / 5;
$CurX = $Padding + $i * $WidthPerChar;
$CurY = mt_rand($CurFontSize+$Padding, $Height - $Padding);
//write text
imagettftext($Image, $CurFontSize, $CurAngle, $CurX, $CurY, 0x626262
, $CurFont, $CurChar); }
//insert the Background
imagecopymerge($Image, $ImageBg,0
,0
,mt_rand(0
,$MaxXOffset), mt_rand(0
,$MaxYOffset),$Size[0
],$Size[1
],mt_rand(60
,70
));
//To compare the Hash with passed Data, save hash to session
$_SESSION["Captcha"] = md5($Salt.$cap.$Salt);
header('Content-type: image/png'); ?>
To compare, if the
৺captcha was entered correct, use sth like that:
if ($_SESSION["Captcha"] == md5($Salt.$_POST["Captcha"].$Salt){ echo "captcha correct";
}else{
echo "captcha wrong";
}
Hint:
Some "Brute-Force"-Bots are looking if the submit was successful - so if it wasn't, include a pic named "success_reply.png" and write "in the Picture" that the capture code was wrong.
(Humans will be able to read the text in the Picture, but the bot will "find" the words "success" in sourcecode)
Update:
I added the nocache headers, and changed the "rand" to "mt_rand" - Why?
Have a look at my post
৺rand or mt_rand.