php图片验证码

 做用户注册和登录时要用到所以记下.

check.php

<?php
session_start();
function random($lenrn)
{
$strcode="abckdefghijklmnopqrstuvwxyz0123456789";
mt_srand();
$strran="";
for($i=0;$i<$lenrn;$i++){
$strran.=$strcode[mt_rand(0,35)];
}
return strtoupper($strran);
}
$str=random(4); //随机生成的字符串
$width = 50; //验证码图片的宽度
$height = 25; //验证码图片的高度
@header("Content-Type:image/png");
$_SESSION["code"] = $str;
$image=imagecreate($width,$height);
//背景颜色
$back=imagecolorallocate($image,0xFF,0xFF,0xFF);
//模糊点颜色
$pix=imagecolorallocate($image,187,230,247);
//字体颜色
$font=imagecolorallocate($image,41,163,238);
//绘模糊作用的点
mt_srand();
for($i=0;$i<1000;$i++)
{
imagesetpixel($image,mt_rand(0,$width),mt_rand(0,$height),$pix);
}
imagestring($image, 5, 7, 5,$str, $font);
imagerectangle($image,0,0,$width-1,$height-1,$font);
imagepng($image);
imagedestroy($image);
?>

test.php

 

<?php
$errormesage="验证码错误";
session_start();
echo "<img src=check.php border=0 align=absbottom>";//生成图片

if($_SESSION["code"]!=$_POST["textfield"])
{
echo "$errormesage";

}
echo <<<EOT
<form id="form1" name="form1" method="post" action="">
  <label>
  <input type="text" name="textfield" id="textfield" />
  </label>
  <label>
  <input type="submit" name="button" id="button" value="提交" />
  </label>
</form>
EOT;
?>

 

 

posted @ 2009-06-04 13:15  Jcool  阅读(409)  评论(0编辑  收藏  举报