码迷,mamicode.com
首页 > Web开发 > 详细

PHP实现验证码制作

时间:2018-07-18 16:58:25      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:cap   ges   字体   大小写   math   body   title   exit   getc   

captcha.php(PHP产生验证码并储存Session):

 1 <?php
 2  
 3  
 4  
 5 //开启Session
 6 session_start();
 7  
 8  
 9  
10 //绘制底图
11 $image = imagecreatetruecolor(100, 30);//返回资源型的值
12 $bgcolor = imagecolorallocate($image, 255, 255, 255);//创建一个底图
13 imagefill($image, 0, 0, $bgcolor);//区域填充
14  
15  
16  
17 /*
18 //输出随机数字
19 for($i = 0; $i < 4; $i++){
20     $fontsize = 6;//字体大小
21     $fontcolor = imagecolorallocate($image, rand(0, 120), rand(0, 120), rand(0, 120));//字体颜色
22     $fontcontent = rand(0, 9);//字符串内容
23      
24     $x = ($i*100/4) + rand(5, 10);//数字的横坐标
25     $y = rand(5, 10);//数字的纵坐标
26      
27     imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);//在图像资源上绘制字符
28 }
29 */
30  
31  
32  
33 //产生随机字符串
34 $captch_code = ‘‘;
35 for($i = 0; $i < 4; $i++){
36     $fontsize = 6;//字体大小
37     $fontcolor = imagecolorallocate($image, rand(0, 120), rand(0, 120),rand(0, 120));//字体颜色
38  
39     $data = 23456789ABCDEFGHJKLMNOPQRTUVWXYZabcdefghjkmnopqrtuvwxy;//随机字符串的字典
40     $fontcontent = substr($data, rand(0, strlen($data)), 1);//字符
41     $captch_code .= $fontcontent;//拼接字符串
42      
43     $x = ($i*100/4) + rand(5, 10);//字符横坐标
44     $y = rand(5, 10);//字符纵坐标
45      
46     //在图像资源上绘制字符
47     imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
48 }
49 //将验证码字符串存储在Session
50 $_SESSION[authcode] = $captch_code;
51  
52  
53  
54  
55 //点干扰
56 for($i = 0; $i < 200; $i++){
57     $pointcolor = imagecolorallocate($image, rand(50, 200), rand(50, 200), rand(50, 200));
58     imagesetpixel($image, rand(1, 99), rand(1, 99), $pointcolor);
59 }
60  
61  
62  
63 //线干扰
64 for($i = 0; $i < 6; $i++){
65     $linecolor = imagecolorallocate($image, rand(80, 220), rand(80, 220), rand(80, 220));
66     imageline($image, rand(1, 99), rand(1, 99), rand(1, 99), rand(1, 99), $linecolor);
67 }
68  
69  
70  
71 //输出图片内容
72 header(content-type:image/png);//输出内容的格式
73 imagepng($image);//输出内容
74 imagedestroy($image);//销毁资源
75  
76  
77  
78  
79  
80 ?>

 

 

 

captcha-form.html(Web表单验证):

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4   <meta charset="utf-8">
 5   <title>验证码</title>
 6 </head>
 7  
 8 <body>
 9   <form method="post" action="./captcha-result.php">
10      
11     <p>
12       验证码图片:
13       <img id="captcha-img" border="1" src="./captcha.php?r=0">
14       <a href="javascript:void(0);" onclick="getCaptchaImg();">看不清</a>
15     </p>
16      
17     <p>请输入图片中的内容:<input id="authcode" type="text" name="authcode" value=""></p>
18      
19     <p><input type="submit" value="提交" style="padding:6px 20px;"></p>
20    
21   </form>
22  
23   <script>
24     //动态获取验证码
25     function getCaptchaImg(){
26         //从服务器获取新的验证码
27         document.getElementById(captcha-img).src=./captcha.php?r=+Math.random();
28         //清空文本框里已输入的内容
29         document.getElementById(authcode).value="";
30     }
31   </script>
32  
33 </body>
34 </html>

 

技术分享图片

 

captcha-result.php(PHP判断验证码是否正确):

 1 <?php
 2  
 3  
 4  
 5 //验证验证码是否正确
 6 if(isset($_REQUEST[‘authcode‘])){
 7     //开启Session
 8     session_start();
 9      
10     //strtolower()将字符串都转换成小写,将验证码设置为不区分大小写型
11     if(strtolower($_REQUEST[‘authcode‘]) == strtolower($_SESSION[‘authcode‘])){
12         echo "输入正确";
13     }else{
14         echo "输入错误";
15     }
16     exit();
17 }
18  
19  
20  
21 ?>

 

 

所用到的函数原型:

 1 <?php
 2  
 3  
 4  
 5 //imagecreatetruecolor() 返回一个图像标识符,代表了一幅大小为 x_size 和 y_size 的黑色图像。
 6 resource imagecreatetruecolor ( int $width , int $height );
 7  
 8  
 9  
10 //imagecolorallocate() 返回一个标识符,代表了由给定的 RGB 成分组成的颜色。red,green 和 blue 分别是所需要的颜色的红,绿,蓝成分。
11 int imagecolorallocate ( resource $image , int $red , int $green , int $blue );
12  
13  
14  
15 //imagefill() 在 image 图像的坐标 x,y(图像左上角为 0, 0)处用 color 颜色执行区域填充(即与 x, y 点颜色相同且相邻的点都会被填充)。
16 bool imagefill ( resource $image , int $x , int $y , int $color );
17  
18  
19  
20 //imagestring() 用 col 颜色将字符串 s 画到 image 所代表的图像的 x,y 坐标处(这是字符串左上角坐标,整幅图像的左上角为 0,0)。如果 font 是 1,2,3,4 或 5,则使用内置字体。
21 bool imagestring ( resource $image , int $font , int $x , int $y , string $s , int $col );
22  
23  
24  
25 //imagesetpixel() 在 image 图像中用 color 颜色在 x,y 坐标(图像左上角为 0,0)上画一个点。
26 bool imagesetpixel ( resource $image , int $x , int $y , int $color );
27  
28  
29  
30 //imageline() 用 color 颜色在图像 image 中从坐标 x1,y1 到 x2,y2(图像左上角为 0, 0)画一条线段。
31 bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color );
32  
33  
34  
35 //imagepng() 将 GD 图像流(image)以 PNG 格式输出到标准输出(通常为浏览器),或者如果用 filename 给出了文件名则将其输出到该文件。
36 bool imagepng ( resource $image [, string $filename ] );
37  
38  
39  
40 //imagedestroy() 释放与 image 关联的内存。image 是由图像创建函数返回的图像标识符,例如 imagecreatetruecolor()。
41 bool imagedestroy ( resource $image );
42  
43  
44  
45 ?>

 

PHP实现验证码制作

标签:cap   ges   字体   大小写   math   body   title   exit   getc   

原文地址:https://www.cnblogs.com/tong707/p/php-captcha-image.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!