这篇文章主要为大家详细介绍了php实现动态口令认证的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
谷歌身份验证器Google Authenticator是谷歌推出的一款动态口令工具,解决大家各平台账户遭到恶意攻击的问题,一般在相关的服务平台登陆中除了用正常用户名和密码外,需要再输入一次谷歌认证器生成的动态口令才能验证成功,相当于输入二次密码,以达到账户的高安全性。
例如交易所、金融平台、以及一些钱包等项目等等,都会使用谷歌身份验证器Google Authenticator来做二次认证,开启谷歌身份验证之后,登录账户,除了输入用户名和密码,还需要输入谷歌验证器上的动态密码。谷歌验证器上的动态密码,也称为一次性密码,密码按照时间或使用次数不断动态变化(默认 30 秒变更一次)
代码参考:https://github.com/PHPGangsta/GoogleAuthenticator
关键代码:
<?php// https://github.com/PHPGangsta/GoogleAuthenticatorerror_reporting(0);// 关闭错误报告session_start(); // 启动session require_once 'PHPGangsta/GoogleAuthenticator.php';$ga = new PHPGangsta_GoogleAuthenticator();// $secret = $ga->createSecret();// 自定义安全密钥$secret = "62H6TMAXQTZBVTRB";// 手机端扫描二维码获取动态口令$qrCodeUrl = $ga->getQRCodeGoogleUrl('username', $secret);echo "二维码地址: ".$qrCodeUrl."\n\n";// 输出动态口令$oneCode = $ga->getCode($secret);echo "本次登录的动态口令:'$oneCode'\n";// 动态口令认证$checkResult = $ga->verifyCode($secret, $password,2); // 2 = 2*30sec clock toleranceif ($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
将