本款php生成验证码代码原理生成随机数-->创建图片-->随机数写进图片-->保存在session中,看一下流程验证码图片生成 通知浏览器将要输出PNG图片,准备好随机数发生器种子 srand((double)microtime()*1000000); 将四位整数验证码绘入图片.
php加入干扰的生成验证码代码如下:
-
-
-
- function random($length) {
- $hash = '';
- $chars = 'abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz';
- $max = strlen($chars) - 1;
- mt_srand((double)microtime() * 1000000);
- for($i = 0; $i < $length; $i++) {
- $hash .= $chars[mt_rand(0, $max)];
- }
- return $hash;
- }
-
-
-
- session_start();
-
- header("content-type: image/png");
-
-
-
- $im = imagecreate(62,22);
- $black = imagecolorallocate($im, 0,0,0);
- $white = imagecolorallocate($im, 255,255,255);
- $gray = imagecolorallocate($im, 179,183,185);
-
- imagefill($im,0,0,$gray);
-
-
-
- $randval=random(4);
- $_session["check_code"]=$randval;
- imagestring($im, 5, 10, 3, $randval, $white);
-
-
- for($i=0;$i<150;$i++){
- $randcolor = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
- imagesetpixel($im, rand()%70 , rand()%30 , $white);
- }
-
- imagepng($im);
-
- imagedestroy($im);
|