PHP验证码

<?php


//随机生成一个4位数字的验证码
$num='';
for($i=0;$i<4;$i++){
$num.=dechex(rand(0,9)); //dechex函数是十进制转会二进制
}

session_start(); //开启session
$_SESSION['verify']=$num; //用session记住这个验证数字
header("Content-type:image/PNG");
$im=imagecreate(60,20); //创建一个画布
$back=imagecolorallocate($im,rand(0,55),rand(0,20),rand(0,5));//创建一个背景颜色 (黑色)
$gray=imagecolorallocate($im,rand(0,255),rand(0,200),rand(0,55)); //(白色)
imagefill($im,0,0,$gray); //填充颜色
$style=array($back,$back,$back,$back,$back,$gray,$gray,$gray,$gray,$gray); //生成数组
imagesetstyle($im,$style); //设定画线风格
$y1=rand(0,20);
$y2=rand(0,20);
$y3=rand(0,20);
$y4=rand(0,20);
imageline($im,0,$y1,60,$y3,IMG_COLOR_STYLED); //画一条线
imageline($im,0,$y2,60,$y4,IMG_COLOR_STYLED);
//在画布上随机生成大量黑点,起干扰作用
for ($i=0;$i<80;$i++){
imagesetpixel($im,rand(0,60),rand(0,20),$back);
}
$str=rand(3,8);
for ($i=0;$i<4;$i++){
$strp=rand(1,6);
imagestring($im,6,$str,$strp,substr($num,$i,1),$back);
$str+=rand(8,12);
}
ImagePNG($im);
imagedestroy($im);
?>
原文地址:https://www.cnblogs.com/xchaos/p/2378774.html