思路
1.用php生成验证码文件 xx.php 用图片输出,同时在session 存储这个str
2.在登入界面 src引入这个图片 [ img src="vcode.php" οnclick="this.src='vcode.php?id='+Math.random()" ] 点击刷新 增加参数随机数
3.判断界面 check 转小写 删除空格 通过session判断验证码正确性[ $ncode=strtolower(str_replace(' ','',$code));]
<?php
// 1.创建画布资源
$img=imagecreatetruecolor(150,50);
// 2.准备颜色
$black=imagecolorallocate($img,0,0,0);
$white=imagecolorallocate($img,255,255,255);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
$gray=imagecolorallocate($img,180,180,180);
// 3.填充画布
imagefill($img,0,0,$black);
$arr=array_merge(range(0,9),range(a,z),range(A,Z));
shuffle($arr);
$str=join(' ',array_slice($arr,0,4));
//画字
imagettftext($img,20,0,20,35,$white,'ms.ttf',$str);
//干扰素
for($i=0;$i<30;$i++){
imagearc($img,mt_rand(0,150),mt_rand(0,50),mt_rand(0,150),mt_rand(0,50),mt_rand(0,360),mt_rand(0,360),$white);
}
// 5.输出最终图像或保存最终图像
header('content-type:image/png');
// 图片从浏览器上输出
imagepng($img);
// 把图片保存到本地
// imagejpeg($img,'jin.jpg');
// 6.释放画布资源
imagedestroy($img);
?>