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();
}
}