PHP之验证码的实现

简单的用PHP实现验证码:

 1 ?php
 2 /**
 3 
 4 *制作验证码
 5 *1.启动session
 6 *2.设定标头
 7 *3.创建画布
 8 *4.创建颜色
 9 *5.创建随机数并放到画布上
10 *6.将得到的若干随机数放入session中
11 *7.添加干扰点或干扰线
12 *8.输出画布
13 *9.销毁画布资源
14 */
15 
16 //1.启动session
17 session_start();
18 //2.设定标头指定MIME输出类型
19 header('Content-Type:image/png');
20 //3.创建画布
21 $width = 100;
22 $height = 30;
23 $im = imagecreate($width,$height);
24 
25 //4.创建颜色
26 $bgcolor = imagecolorallocate($im,255,255,255);
27 $textcolor = imagecolorallocate($im,0,255,255);
28 $randcolor = imagecolorallocate($im,mt_rand(0,200),mt_rand(0,200),mt_rand(0,200));
29 
30 //5.创建随机数并放到画布上
31 $verify=null;
32 for($i=0;$i<4;$i++){
33 $temp = mt_rand(0,9);
34 $verify.=$temp;
35 imagestring($im,5,$i*15+15,8,$temp,imagecolorallocate($im,mt_rand(0,200),mt_rand(0,200),mt_rand(0,200)));
36 }
37 
38 
39 //6将生成的随机数放入session中
40 $_SESSION['verify'] = $verify;
41 
42 //7.添加干扰点
43 for($i=0;$i<100;$i++){
44 imagesetpixel($im,rand(0,$width),rand(0,$height),imagecolorallocate($im,rand(100,255),rand(100,255),rand(100,255)));
45 }
46 
47 //8.将图像输出
48 imagepng($im);    //imagegif()
49 //9.销毁一图像
50 imagedestroy($im);
51 
52 ?>
53  
原文地址:https://www.cnblogs.com/Steven-shi/p/5457033.html