我是靠谱客的博主 默默小猫咪,这篇文章主要介绍PHP生成图形验证码(加强干扰型),现在分享给大家,希望可以做个参考。

验证码使用场景

我们在开发系统的过程中,基本所有的系统都会涉及到登录模块,其中验证码功能是这里面必不可少的一块,是防止系统被爆破的有效途径。所谓道高一尺魔高一丈,现在的验证码越来越复杂先进,常见的字母数字验证码,行为验证码。本文详细介绍简单的字母数字验证码。

代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php /********************************************************************************* * InitPHP 3.8.2 国产PHP开发框架 扩展类库-验证码 *------------------------------------------------------------------------------- * 版权所有: CopyRight By initphp.com * 您可以自由使用该源码,但是在使用过程中,请保留作者信息。尊重他人劳动成果就是尊重自己 *------------------------------------------------------------------------------- * Author:zhuli Dtime:2014-11-25 ***********************************************************************************/ class Code { private $charset = "abcdefghjklmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789"; //随机因子 private $code; //验证码文字 private $codelen = 4; //验证码显示几个文字 private $width = 100; //验证码宽度 private $height = 40; //验证码高度 private $img; //验证码资源句柄 private $font; //指定的字体 private $fontsize = 20; //指定的字体大小 private $fontcolor; //字体颜色 随机 //构造类 编写字体 public function __construct() { $this->font = '/outputs/font/font.ttf'; } //创建4个随机码 private function createCode() { $_leng = strlen($this->charset) - 1; for ($i = 1; $i <= $this->codelen; $i++) { $this->code .= $this->charset[mt_rand(0, $_leng)]; } // session_start(); // $_SESSION['VerifyCode'] = strtolower($this->code); Session::set('VerifyCode', strtolower($this->code)); return $this->code; } //创建背景 private function createBg() { //创建画布 给一个资源jubing $this->img = imagecreatetruecolor($this->width, $this->height); //背景颜色 $color = imagecolorallocate($this->img, mt_rand(157, 255), mt_rand(157, 255), mt_rand(157, 255)); //画出一个矩形 imagefilledrectangle($this->img, 0, $this->height, $this->width, 0, $color); } //创建字体 private function createFont() { $_x = ($this->width / $this->codelen); //字体长度 for ($i = 0; $i < $this->codelen; $i++) { //文字颜色 $color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156)); //资源句柄 字体大小 倾斜度 字体长度 字体高度 字体颜色 字体 具体文本 imagettftext($this->img, $this->fontsize, mt_rand(-30, 30), $_x * $i + mt_rand(1, 5), $this->height / 1.4, $color, $this->font, $this->code[$i]); } } //随机线条 private function createLine() { //随机线条 for ($i = 0; $i < 6; $i++) { $color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156)); imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $color); } //随机雪花 for ($i = 0; $i < 45; $i++) { $color = imagecolorallocate($this->img, mt_rand(220, 255), mt_rand(220, 255), mt_rand(220, 255)); imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), '*', $color); } } //输出背景 private function outPut() { //生成标头 header('Content-type:image/png'); //输出图片 imagepng($this->img); //销毁结果集 imagedestroy($this->img); } //对外输出 public function doimg() { //加载背景 $this->createBg(); //加载文件 $this->createCode(); //加载线条 $this->createLine(); //加载字体 $this->createFont(); //加载背景 $this->outPut(); } //获取验证码 public function getCode() { return strtolower($this->code); } //验证验证码 public function checkCode($code, $clear = false) { // session_start(); if (Session::get('VerifyCode') == strtolower($code)) { if($clear) $this->clearCode(); return true; } if($clear) $this->clearCode(); return false; } //清除验证码 public function clearCode() { Session::del('VerifyCode'); // session_start(); // unset ($_SESSION['VerifyCode']); } }
登录后复制

验证

复制代码
1
2
3
ob_clean(); $verify = new Code(); $verify->doimg();
登录后复制

这样即可输出如下验证码

WX20200429-205016.png

可以调整参数控制验证码的大小,干扰项等。

拓展

接下来介绍下拓展的功能,怎么加强验证码的干扰项,怎么结合到项目丽进行登录验证。

1. 加强干扰

首先我们可以看到上面的截图中少数线条,如果外者使用分析工具来解码,那么会很简单的就解出我们的验证码,这时候就需要添加线条的数量,在代码中找到以下代码并修改

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//随机线条 private function createLine() { //随机线条 for ($i = 0; $i < 6; $i++) { $color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156)); imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $color); } //随机雪花 for ($i = 0; $i < 45; $i++) { $color = imagecolorallocate($this->img, mt_rand(220, 255), mt_rand(220, 255), mt_rand(220, 255)); imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), '*', $color); } }
登录后复制

上面的数字6可以慢慢调整,然后查看效果直到满意。同时可以看到验证码中有很多雪花效果,这个也是干扰项,可以修改上面的数字45来调整到自己满意的结果。

2. 接入项目验证

新建个文件,代码如下

复制代码
1
2
3
4
<?php ob_clean(); $verify = new Code(); $verify->doimg();
登录后复制

然后在现有的系统登录页面引入这个接口即可展示验证码,在用户填写提交之后,服务端做以下验证

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//验证验证码 public function checkCode($code, $clear = false) { if (Session::get('VerifyCode') == strtolower($code)) { if($clear) $this->clearCode(); return true; } if($clear) $this->clearCode(); return false; } //清除验证码 public function clearCode() { Session::del('VerifyCode'); }
登录后复制

至此,验证码的生成以及验证流程都已完成。

以上就是PHP生成图形验证码(加强干扰型)的详细内容,更多请关注靠谱客其它相关文章!

最后

以上就是默默小猫咪最近收集整理的关于PHP生成图形验证码(加强干扰型)的全部内容,更多相关PHP生成图形验证码(加强干扰型)内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(89)

评论列表共有 0 条评论

立即
投稿
返回
顶部