PHP之验证码代码

时间:2023-03-09 17:22:21
PHP之验证码代码
<?php
session_start();
$checkcode="";
/*for($i=0;$i<4;$i++)
{
$checkcode.=dechex(rand(1,15));
//$checkcode.=rand(0,9);
}*/
function getStr($len = 4){
$str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$s = '';
for($i=0;$i<$len;$i++){
$s.=$str{mt_rand(0,strlen($str)-1)};
}
return $s;
}
$checkcode=getstr(); //产生随机4位的验证码
$_SESSION['checkcode']=$checkcode; //存入session $image1=imagecreatetruecolor(110,30); //创建画布 $red=imagecolorallocate($image1,255,255,255); //定义颜色 for($i=0;$i<10;$i++) //干扰线
{
imageline($image1,rand(0,110),rand(0,30),rand(0,110),rand(0,30),imagecolorallocate($image1,rand(0,255),rand(0,255),rand(0,255)));
} imagestring($image1,rand(3,8),rand(0,80),rand(0,15),$checkcode,$red); //生成图片
header("content-type:image/png");
imagepng($image1); //输出图片
imagedestroy($image1); //销毁图片
?>