Date: November 27 2006 01:07 AM
Removed member logins and placed a simpler captcha on forums to keep the spam down. Code:session_start();
ini_set('session.gc_maxlifetime',60*10); //10 minutes
$width = 140;
$height = 26;
// make code
$codelen = rand(4,7);
$validchars = "389ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghjkmnopqrstuvwxyz";
$code = "";
for($i = 0; $i<$codelen; $i++)
{
$code.=substr($validchars,rand(0,strlen($validchars)-1),1);
}
// save nc/lower hash in current session
$_SESSION['captcha'] = md5(strtolower($code));
// create blank image
$image = imagecreatetruecolor($width, $height);
function random_color($image, $a,$b)
{
return imagecolorallocate($image, rand($a,$b), rand($a,$b), rand($a,$b));
}
// colored sectional background
$R=0x00;
$wd = 20;
$x = 0;
$y=0; //?
while ($x < $width)
{
$wd = max(2, rand(0, 20) - 2);
imagefilledrectangle($image, $x, 0, $x+$wd, $height, random_color($image, 222^$R, 255^$R));
$x+=$wd;
}
// display code
$fonts=glob(dirname(__FILE__)."/*.ttf");
if(count($fonts)>1)
{
for($i=0; $i<$codelen; $i++)
{
$em=$width/($codelen+1);
$letter=substr($code,$i,1);
$font = $fonts[rand(0, count($fonts)-1)];
$rotation = rand(-33, 33);
$rotation2 = $rotation+rand(-7, 7);
$size = rand(18, $height/2.2);
$x=($em/2)+$em*$i+rand(4,7);
$y = rand($size+3, $height-3);
$r=rand(26,97);
$g=$r; //rand(30,99);
$b=rand(26,97);
$R=0xff;
$c1 = imagecolorallocate($image, $r*1^$R, $g*1^$R, $b*1^$R);
$c2 = imagecolorallocate($image, $r*2^$R, $g*2^$R, $b*2^$R);
imagettftext($image, $size, $rotation, $x+1, $y, $c2, $font, $letter);
imagettftext($image, $size, $rotation2, $x, $y-1, $c1, $font, $letter);
}
}
// faint border
$bordercolor = imagecolorallocate($image, 64, 64, 64);
imagerectangle($image,0,0,$width-1,$height-1,$bordercolor);
// do not cache
header("Expires: 1 Jan 1970 00:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
// output image
header('Content-type: image/gif');
imageGIF($image);
imageDestroy($image);
?>
|