GD图形库写了个验证码

类别:编程语言 点击:0 评论:0 推荐:
1、请输入右边的验证码:
              <input name="auth_number" type="text" size="4" maxlength="4" class="input1">
              <img src="auth_img.php" width="80" height="20" border=0 alt="">

2、auth_img.php
<?
session_start();

$img_width=80;
$img_height=20;

srand(microtime() * 100000);
for($ti=0;$ti<4;$ti++)
{
$new_number.=dechex(rand(0,15));
}

$_SESSION[check_auth_number]=$new_number;
$number_img=imageCreate($img_width,$img_height);
ImageColorAllocate($number_img,255,255,255);
for($i=1;$i<=128;$i++)
{
imageString($number_img,1,mt_rand(1,$img_width),mt_rand(1,$img_height),"*",imageColorAllocate($number_img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)));
}
for($i=0;$i<strlen($HTTP_SESSION_VARS[check_auth_number]);$i++)
{
imageString($number_img,mt_rand(3,5),$i*$img_width/4+mt_rand(1,8),mt_rand(1,$img_height/4), $HTTP_SESSION_VARS[check_auth_number][$i],imageColorAllocate($number_img,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200)));
}
header("Content-type: image/png");
ImagePng($number_img);
ImageDestroy($number_img);
?>

3、验证部份
function check_auth_number($auth_number)
{
if($auth_number!=$_SESSION[check_auth_number] || empty($_SESSION[check_auth_number]))
{
echo "<Script Language =\"JavaScript\">\n history.back();\n alert(\" 图片验证码错误 \");\n </Script> \n";
exit();
}
}

本文地址:http://com.8s8s.com/it/it26846.htm