验证码实现
1.根据提供字母或数字随机生成四位数验证码
2.将验证码存入session中
3.将验证码放入图片中
//根据提供字符生成四位验证码
$str = 'a,b,c,d,e,f,g,h,m,n,p,q,l,r,t,2,3,4,5,6,7,8,9,';
$list = explode(',', $str);
$max = count($list)-1;
$verifyCode = '';
for ($i=0; $i < 4; $i++) {
$randnum = mt_rand(0,$max);
$verifyCode .= $list[$randnum];
}
//存进session
$_SESSION['code'] = $verifyCode;
//设置变量从0到70和从0到30的随机数
$rand_x = rand(0,70);
$rand_y = rand(0,30);
$color = rand(0,200);
//创建画布
$img = imagecreatetruecolor(70, 30);
$blue = imagecolorallocate($img, 100, 190, 100);
$block = imagecolorallocate($img, 255, 255, 255);
//干扰元素颜色
$red = imagecolorallocate($img, 255, 0, 0);
$blue = imagecolorallocate($img, 0, 0, 255);
$green = imagecolorallocate($img, 0, 255, 0);
//干扰元素
for($i=1;$i<50;$i++){
imagesetpixel($img, rand(0,70), rand(0,30), $blue);
imagesetpixel($img, rand(0,70), rand(0,30), $red);
imagesetpixel($img, rand(0,70), rand(0,30), $green);
}
imagearc($img, $rand_x, $rand_y, $rand_x, $rand_y, $rand_x, $rand_y, $red);
imageline($img, $rand_x, $rand_y, $rand_x, $rand_y, $green);
//设置背景填充色
imagefill($img, 0, 0, $blue);
//把验证码放进画布
imagestring($img, 18, 10, 10, $verifyCode, $block);
header('Content-Type:image/png');
//设置图片格式
imagepng($img);
imagedestroy($img);
135

被折叠的 条评论
为什么被折叠?



