PHP验证码实现原理
生成随机数或者字母保存到session中(验证验证码的时候用),然后对生成的数字或者字母进行绘图!然后呈现在我们眼前
刷新验证码:用js改变验证码图片所带的参数,让浏览器不读缓存的图片,从而实现刷新验证码效果!
代码示例
- $str="QWERTYUIOPASDFGHJKLZXCVBNM1234567890";
-
- $image=imagecreate(50,25);
- imagecolorallocate($image,mt_rand(0,125),mt_rand(0,125),mt_rand(0,125));
- $color = imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
- for($i=1;$i$date=$str[mt_rand(0,strlen($str)-1)];
- $code.=$date;
- }
- session_start();
- $_SESSION['code'] = $code;
- imagestring($image,4,8,4,$code,$color);
- for($i=1;$iimagesetpixel($image,mt_rand(0,50),mt_rand(0,25),mt_rand(125,200));
- }
- for($i=1;$i
- 数字+字母验证码(各字母颜色不同):
-
-
$str="QWERTYUIOPASDFGHJKLZXCVBNM1234567890";
- $image=imagecreate(50,25);
- imagecolorallocate($image,mt_rand(0,125),mt_rand(0,125),mt_rand(0,125));
- $color[0] = imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
- $color[1] = imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
- $color[2] = imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
- $color[3] = imagecolorallocate($image,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
- for($i=0;$i$date=$str[mt_rand(0,strlen($str)-1)];
- $code.=$date;
- imagestring($image,5,6+$i*10,4,$code[$i],$color[$i]);
- }
- session_start();
- $_SESSION['code'] = $code;
- for($i=1;$iimagesetpixel($image,mt_rand(0,50),mt_rand(0,25),mt_rand(125,200));
- }
- for($i=1;$iimageline($image,mt_rand(0,50),mt_rand(0,25),mt_rand(0,50),mt_rand(0,25),mt_rand(100,150));
- }
- header("content-type:image/png");
- imagepng($image);
复制代码
来自:php验证码实现原理
|