Captcha Efficiency
Posted: Sat Dec 15, 2007 9:54 pm
captcha/captcha.php
Then, in my register.php page (which has session_start() at the beginning)
Outputs something like:

I have two questions about this code.
The first is: how efficient is it to draw 5 lines like that with 4 calls to rand() in each one... Is there a better way?
The second is: Is this a decent captcha? Or should it be more awesome in some way (suggestions for improvement needed, that is)?
Code: Select all
<?php
// Much thanks to
// http://www.codewalkers.com/c/a/Miscella ... -with-PHP/
// for the great tutorial on this.
putenv('GDFONTPATH=' . realpath('.'));
$font = 'Acidic';
// Above lines because php manual said to
// http://us.php.net/imagettftext
$md5_hash = md5(rand(0,999));
$code = substr($md5_hash, 15, 7);
$_SESSION['code'] = $code;
// Generate pseudo random string 7 characters long
$width = 130;
$height = 60;
$image = ImageCreate($width, $height);
// Create image
$white = ImageColorAllocate($image, 255, 255, 255);
$black = ImageColorAllocate($image, 0, 0, 0);
$grey = ImageColorAllocate($image, 204, 204, 204);
$red = ImageColorAllocate($image, 255, 0, 0);
// Create colors
ImageFill($image, 0, 0, $black);
imagettftext($image, 18, rand(-10,10), rand(2,35), rand(32,45), $white, $font, $code);
imageline($image, rand(2,128), rand(2, 58), rand(2,128), rand(2, 58), $red);
imageline($image, rand(2,128), rand(2, 58), rand(2,128), rand(2, 58), $red);
imageline($image, rand(2,128), rand(2, 58), rand(2,128), rand(2, 58), $red);
imageline($image, rand(2,128), rand(2, 58), rand(2,128), rand(2, 58), $red);
imageline($image, rand(2,128), rand(2, 58), rand(2,128), rand(2, 58), $red);
ImageRectangle($image,0,0,$width-1,$height-1,$grey);
ImageRectangle($image,0,0,$width-2,$height-2,$grey);
// Draw stuff
header("Content-Type: image/jpeg");
// Modify headers for FUN. >.>
ImageJpeg($image);
ImageDestroy($image);
// That's right. Destroy it.
?>Code: Select all
<?php
if(isset($_GET['reg']) && $_GET['reg']==yep) // show register form
{
?>
<img id="captcha" src="captcha/captcha.php" />
<?php
}
?>
I have two questions about this code.
The first is: how efficient is it to draw 5 lines like that with 4 calls to rand() in each one... Is there a better way?
The second is: Is this a decent captcha? Or should it be more awesome in some way (suggestions for improvement needed, that is)?
