php实现动态口令认证的示例代码

谷歌身份验证器Google Authenticator是谷歌推出的一款动态口令工具,解决大家各平台账户遭到恶意攻击的问题,一般在相关的服务平台登陆中除了用正常用户名和密码外,需要再输入一次谷歌认证器生成的动态口令才能验证成功,相当于输入二次密码,以达到账户的高安全性。

例如交易所、金融平台、以及一些钱包等项目等等,都会使用谷歌身份验证器Google Authenticator来做二次认证,开启谷歌身份验证之后,登录账户,除了输入用户名和密码,还需要输入谷歌验证器上的动态密码。谷歌验证器上的动态密码,也称为一次性密码,密码按照时间或使用次数不断动态变化(默认 30 秒变更一次)

代码参考:https://github.com/PHPGangsta/GoogleAuthenticator

关键代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
createSecret();
// 自定义安全密钥
$secret = "62H6TMAXQTZBVTRB";
// 手机端扫描二维码获取动态口令
$qrCodeUrl = $ga->getQRCodeGoogleUrl('username', $secret);
echo "二维码地址: ".$qrCodeUrl."nn";
// 输出动态口令
$oneCode = $ga->getCode($secret);
echo "本次登录的动态口令:'$oneCode'n";
// 动态口令认证
$checkResult = $ga->verifyCode($secret, $password,2);    // 2 = 2*30sec clock tolerance
if ($checkResult) {
    $_SESSION['username'] = $username;
    echo "<h1>登录成功!</h1>";
    header("Refresh: 5; url=main.php");
    exit;
} else {
    echo "<h1>登录失败!</h1>";
    header("Refresh: 3; url=login.html");
    exit;
}
?>

使用方法:

手机端安装 Microsoft Authenticator

下载地址:https://www.microsoft.com/en-us/security/mobile-authenticator-app

将以上代码生成的二维码地址在浏览器中访问

手机端扫描二维码获取动态验证码

代码示例:

login.html

1
2
3
4
5
6
<title>系统运维管理平台</title><div id="login">
    <h1>Login</h1>
     
        <button class="but" type="submit">登录</button>
     
</div>

login.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
    <title>系统运维管理平台</title><div id="login">
createSecret();
# 自定义安全密钥
$secret = "62H6TMAXQTZBVTRB";
// $qrCodeUrl = $ga->getQRCodeGoogleUrl('admin', $secret);
// echo "二维码: ".$qrCodeUrl."nn";
 
// 检查用户是否已经登录 
if (isset($_SESSION['username'])) { 
    // 用户已登录,显示用户信息或其他操作 
    header("Refresh: 3; url=main.php");
} else
    if(!isset($_SESSION['num'])){//isset() — 检测num变量是否设置。
        $_SESSION['num'] = 0;
    }
    // 密码输入错误3次,将不允许登录!
    if($_SESSION['num']getCode($secret);
                echo "本次登录的动态口令:'$oneCode'n";
                $checkResult = $ga->verifyCode($secret, $password,2);    // 2 = 2*30sec clock tolerance
                if ($checkResult) {
                    $_SESSION['username'] = $username;
                    echo "<h1>登录成功!</h1>";
                    header("Refresh: 5; url=main.php");
                    exit;
                } else {
                    $_SESSION['num']++;
                    echo "<h1>登录失败!</h1>";
                    header("Refresh: 3; url=login.html");
                    exit;
                }
            }else{
                echo "<h1>登录失败!</h1>";
                header("Refresh: 3; url=login.html");
                exit;
            }
        } else
            header("Location: login.html");
            exit;
        }
    }else{
        echo "<h1>密码输入错误已超过3次,系统已不允许登录!</h1>";
        header("Refresh: 3; url=login.html");
        exit;
    }
}
?>
</div>

main.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
    <title>系统运维管理平台</title><div id="login">
    <?php session_start(); // 启动session
    if (isset($_SESSION['username'])) { 
        echo "<h2>".$_SESSION['username']."您已登录!";
        echo "</h2><h2><a href="logout.php">退出登录</a></h2>";
    } else{
        header("Refresh: 3; url=login.html");
    }
    ?>
 
 
</div>
<p>logout.php</p>
<div class="jb51code"><pre class="brush:php;">
 
    <title>系统运维管理平台</title><div id="login">
     
</div>
<p>login.css</p>
<div class="jb51code"><pre class="brush:css;">html{  
    width: 100%;  
    height: 100%;  
    overflow: hidden;  
    font-style: sans-serif;  
}  
body{  
    width: 100%;  
    height: 100%;  
    font-family: 'Open Sans',sans-serif;  
    margin: 0;  
    background-color: #4A374A;  
}  
#login{  
    position: absolute;  
    top: 50%;  
    left:50%;  
    margin: -150px 0 0 -150px;  
    width: 300px;  
    height: 300px;  
}  
#login h1,h2{  
    color: #fff;  
    /* text-shadow:0 0 10px;    */
    letter-spacing: 1px;  
    text-align: center;  
}  
h1,h2{  
    font-size: 2em;  
    margin: 0.67em 0;  
}  
input{  
    width: 278px;  
    height: 18px;  
    margin-bottom: 10px;  
    outline: none;  
    padding: 10px;  
    font-size: 13px;  
    color: #fff;  
    /* text-shadow:1px 1px 1px;    */
    border-top: 1px solid #312E3D;  
    border-left: 1px solid #312E3D;  
    border-right: 1px solid #312E3D;  
    border-bottom: 1px solid #56536A;  
    border-radius: 4px;  
    background-color: #2D2D3F;  
}  
.but{  
    width: 300px;  
    min-height: 20px;  
    display: block;  
    background-color: #4a77d4;  
    border: 1px solid #3762bc;  
    color: #fff;  
    padding: 9px 14px;  
    font-size: 15px;  
    line-height: normal;  
    border-radius: 5px;  
    margin: 0;  
}
</pre>
</div>
<p>以上就是php实现动态口令认证的示例代码的详细内容,更多关于php动态口令认证的资料请关注IT俱乐部其它相关文章!</p>
<p></p></pre></div>
<div class="lbd_bot clearfix">
                            <span id="art_bot" class="jbTestPos"></span>
                        </div>
<div class="tags clearfix">
                            <i class="icon-tag"></i><p></p>
<ul class="meta-tags">
<li class="tag item"><a href="http://common.jb51.net/tag/php/1.htm" target="_blank" title="搜索关于php的文章" rel="nofollow noopener">php</a></li>
<li class="tag item"><a href="http://common.jb51.net/tag/%E5%8A%A8%E6%80%81/1.htm" target="_blank" title="搜索关于动态的文章" rel="nofollow noopener">动态</a></li>
<li class="tag item"><a href="http://common.jb51.net/tag/%E5%8F%A3%E4%BB%A4/1.htm" target="_blank" title="搜索关于口令的文章" rel="nofollow noopener">口令</a></li>
<li class="tag item"><a href="http://common.jb51.net/tag/%E8%AE%A4%E8%AF%81/1.htm" target="_blank" title="搜索关于认证的文章" rel="nofollow noopener">认证</a></li>
</ul>
</div>
<div class="lbd clearfix">
                            <span id="art_down" class="jbTestPos"></span>
                        </div>
<div id="shoucang"></div>
<div class="xgcomm clearfix">
<h2>相关文章</h2>
<ul>
<li class="lbd clearfix"><span id="art_xg" class="jbTestPos"></span></li>
<li>
<div class="item-inner">
<a href="https://www.2it.club/article/41405.htm" title="PHP中的str_repeat函数在JavaScript中的实现" class="img-wrap" target="_blank" rel="noopener"> <img decoding="async" src="https://www.2it.club/wp-content/uploads/2024/02/frc-1a1b05c64693fbf380aa1344a7812747.png"></a><p></p>
<div class="rbox">
<div class="rbox-inner">
<p><a class="link title" target="_blank" href="https://www.2it.club/article/41405.htm" title="PHP中的str_repeat函数在JavaScript中的实现" rel="noopener">PHP中的str_repeat函数在JavaScript中的实现</a></p>
<div class="item-info">
<div class="js">PHP中有一个函数:String str_repeat($str, num);挺好用的,在 本文为大家介绍下次函数在js中的实现,感兴趣的朋友可以参考下</div>
<p><span class="lbtn" style="float:right"> 2013-09-09 </span>
</p></div>
</div>
</div>
</div>
</li>
<li>
<div class="item-inner">
<a href="https://www.2it.club/article/163834.htm" title="Laravel向公共模板赋值方法总结" class="img-wrap" target="_blank" rel="noopener"> <img decoding="async" src="https://www.2it.club/wp-content/uploads/2024/02/frc-4f55910a645b073bc4fc65dc10dc14bd.png"></a><p></p>
<div class="rbox">
<div class="rbox-inner">
<p><a class="link title" target="_blank" href="https://www.2it.club/article/163834.htm" title="Laravel向公共模板赋值方法总结" rel="noopener">Laravel向公共模板赋值方法总结</a></p>
<div class="item-info">
<div class="js">在本篇文章里小编给大家整理了关于Laravel向公共模板赋值方法以及相关知识点总结,有兴趣的朋友们学习下。</div>
<p><span class="lbtn" style="float:right"> 2019-06-06 </span>
</p></div>
</div>
</div>
</div>
</li>
<li>
<div class="item-inner">
<a href="https://www.2it.club/article/117271.htm" title="PHP API接口必备之输出json格式数据示例代码" class="img-wrap" target="_blank" rel="noopener"> <img decoding="async" src="https://www.2it.club/wp-content/uploads/2024/02/frc-0ea3c7666119d5615e582f823fb3fad6.png"></a><p></p>
<div class="rbox">
<div class="rbox-inner">
<p><a class="link title" target="_blank" href="https://www.2it.club/article/117271.htm" title="PHP API接口必备之输出json格式数据示例代码" rel="noopener">PHP API接口必备之输出json格式数据示例代码</a></p>
<div class="item-info">
<div class="js">这篇文章主要给大家介绍了关于PHP API接口必备之输出json格式数据的相关资料文中通过示例代码介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面来一起看看吧。</div>
<p><span class="lbtn" style="float:right"> 2017-06-06 </span>
</p></div>
</div>
</div>
</div>
</li>
<li>
<div class="item-inner">
<a href="https://www.2it.club/article/58838.htm" title="php实现基于微信公众平台开发SDK(demo)扩展的方法" class="img-wrap" target="_blank" rel="noopener"> <img decoding="async" src="https://www.2it.club/wp-content/uploads/2024/02/frc-4f96a78db829b1556ff16de21e013c7a.png"></a><p></p>
<div class="rbox">
<div class="rbox-inner">
<p><a class="link title" target="_blank" href="https://www.2it.club/article/58838.htm" title="php实现基于微信公众平台开发SDK(demo)扩展的方法" rel="noopener">php实现基于微信公众平台开发SDK(demo)扩展的方法</a></p>
<div class="item-info">
<div class="js">这篇文章主要介绍了php实现基于微信公众平台开发SDK(demo)扩展的方法,包括处理文本消息、天气、翻译、聊天信息及自定义菜单等,需要的朋友可以参考下</div>
<p><span class="lbtn" style="float:right"> 2014-12-12 </span>
</p></div>
</div>
</div>
</div>
</li>
<li>
<div class="item-inner">
<a href="https://www.2it.club/program/308669ye4.htm" title="PHP提供下载功能实现案例" class="img-wrap" target="_blank" rel="noopener"> <img decoding="async" src="https://www.2it.club/wp-content/uploads/2024/02/frc-8cc1031babc6aff2319f1c6af8544aa0.png"></a><p></p>
<div class="rbox">
<div class="rbox-inner">
<p><a class="link title" target="_blank" href="https://www.2it.club/program/308669ye4.htm" title="PHP提供下载功能实现案例" rel="noopener">PHP提供下载功能实现案例</a></p>
<div class="item-info">
<div class="js">这篇文章主要为大家介绍了PHP提供下载功能实现案例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪</div>
<p><span class="lbtn" style="float:right"> 2023-12-12 </span>
</p></div>
</div>
</div>
</div>
</li>
<li>
<div class="item-inner">
<a href="https://www.2it.club/article/62226.htm" title="php压缩和解压缩字符串的方法" class="img-wrap" target="_blank" rel="noopener"> <img decoding="async" src="https://www.2it.club/wp-content/uploads/2024/02/frc-0c932a99bb7b6f23c937db507070cc7b.png"></a><p></p>
<div class="rbox">
<div class="rbox-inner">
<p><a class="link title" target="_blank" href="https://www.2it.club/article/62226.htm" title="php压缩和解压缩字符串的方法" rel="noopener">php压缩和解压缩字符串的方法</a></p>
<div class="item-info">
<div class="js">这篇文章主要介绍了php压缩和解压缩字符串的方法,涉及php中gzcompress与gzuncompress的使用技巧,需要的朋友可以参考下</div>
<p><span class="lbtn" style="float:right"> 2015-03-03 </span>
</p></div>
</div>
</div>
</div>
</li>
<li>
<div class="item-inner">
<a href="https://www.2it.club/article/133392.htm" title="php数据结构之顺序链表与链式线性表示例" class="img-wrap" target="_blank" rel="noopener"> <img decoding="async" src="https://www.2it.club/wp-content/uploads/2024/02/frc-cca732bf65a93ed2ec0ac80c638460fe.png"></a><p></p>
<div class="rbox">
<div class="rbox-inner">
<p><a class="link title" target="_blank" href="https://www.2it.club/article/133392.htm" title="php数据结构之顺序链表与链式线性表示例" rel="noopener">php数据结构之顺序链表与链式线性表示例</a></p>
<div class="item-info">
<div class="js">这篇文章主要介绍了php数据结构之顺序链表与链式线性表,结合实例形式较为详细的分析了php实现顺序链表与链式线性表的各种常用操作技巧,需要的朋友可以参考下</div>
<p><span class="lbtn" style="float:right"> 2018-01-01 </span>
</p></div>
</div>
</div>
</div>
</li>
<li>
<div class="item-inner">
<a href="https://www.2it.club/article/40229.htm" title="基于php中使用excel的简单介绍" class="img-wrap" target="_blank" rel="noopener"> <img decoding="async" src="https://www.2it.club/wp-content/uploads/2024/02/frc-2d9f31f2af7b675a3d153d2b7f1035a7.png"></a><p></p>
<div class="rbox">
<div class="rbox-inner">
<p><a class="link title" target="_blank" href="https://www.2it.club/article/40229.htm" title="基于php中使用excel的简单介绍" rel="noopener">基于php中使用excel的简单介绍</a></p>
<div class="item-info">
<div class="js">我目前使用的是phpexcel1.7.3版本, 解压缩后里面有一个PHPExcel和PHPExcel.php文件。我们主要使用那个PHP文件。见下图文件目录结构 </div>
<p><span class="lbtn" style="float:right"> 2013-08-08 </span>
</p></div>
</div>
</div>
</div>
</li>
<li>
<div class="item-inner">
<a href="https://www.2it.club/article/74285.htm" title="php在数据库抽象层简单使用PDO的方法" class="img-wrap" target="_blank" rel="noopener"> <img decoding="async" src="https://www.2it.club/wp-content/uploads/2024/02/frc-b452cee8ec5cd9e58ab98eba17281e59.png"></a><p></p>
<div class="rbox">
<div class="rbox-inner">
<p><a class="link title" target="_blank" href="https://www.2it.club/article/74285.htm" title="php在数据库抽象层简单使用PDO的方法" rel="noopener">php在数据库抽象层简单使用PDO的方法</a></p>
<div class="item-info">
<div class="js">这篇文章主要介绍了php在数据库抽象层简单使用PDO的方法,以PDO针对数据库的连接、插入、查询等操作为例分析了PDO操作数据库的相关技巧,需要的朋友可以参考下</div>
<p><span class="lbtn" style="float:right"> 2015-11-11 </span>
</p></div>
</div>
</div>
</div>
</li>
<li>
<div class="item-inner">
<a href="https://www.2it.club/article/17428.htm" title="PHP中查询SQL Server或Sybase时TEXT字段被截断的解决方法" class="img-wrap" target="_blank" rel="noopener"> <img decoding="async" src="https://www.2it.club/wp-content/uploads/2024/02/frc-f4838ec7e2d4da28e0b57d4e852dadd4.png"></a><p></p>
<div class="rbox">
<div class="rbox-inner">
<p><a class="link title" target="_blank" href="https://www.2it.club/article/17428.htm" title="PHP中查询SQL Server或Sybase时TEXT字段被截断的解决方法" rel="noopener">PHP中查询SQL Server或Sybase时TEXT字段被截断的解决方法</a></p>
<div class="item-info">
<div class="js">在CSDN的PHP版里老是看到有人问TEXT字段被截断的问题,偶也回答了无数次,今天索性就总结一下吧</div>
<p><span class="lbtn" style="float:right"> 2009-03-03 </span>
</p></div>
</div>
</div>
</div>
</li>
</ul>
</div>
<div class="lbd clearfix mt5">
                            <span id="art_down2" class="jbTestPos"></span>
                        </div>
<p>                        <a href=""></a></p>
<div id="comments">
<h2>最新评论</h2>
<div class="pd5">
<div id="SOHUCS"></div>
<p></p></div>
<p></p></div>
<p></p>
本文收集自网络,不代表IT俱乐部立场,转载请注明出处。https://www.2it.club/code/php/9480.html
上一篇
下一篇
联系我们

联系我们

在线咨询: QQ交谈

邮箱: 1120393934@qq.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部