经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » PHP » 查看文章
PHP设置谷歌验证器(Google Authenticator)实现操作二步验证
来源:cnblogs  作者:洛卡卡了  时间:2019/8/21 9:38:03  对本文有异议

使用说明:开启Google的登陆二步验证(即Google Authenticator服务)后用户登陆时需要输入额外由手机客户端生成的一次性密码。实现Google Authenticator功能需要服务器端和客户端的支持。服务器端负责密钥的生成、验证一次性密码是否正确。客户端记录密钥后生成一次性密码。

下载谷歌验证类库文件放到项目合适位置(我这边放在项目Vender下面)

https://github.com/PHPGangsta/GoogleAuthenticator

PHP代码示例:

 

  1. //引入谷歌验证器类
  2. vendor('googleAuth.GoogleAuthenticator-master.PHPGangsta.GoogleAuthenticator');
  3. $ga = new \PHPGangsta_GoogleAuthenticator();
  4. //这是生成的密钥,每个用户唯一一个,为用户保存起来用于验证
  5. $secret = $ga->createSecret();
  6. //echo $secret;
  7. //下面为生成二维码,内容是一个URI地址(otpauth://totp/账号?secret=密钥&issuer=标题)
  8. $qrCodeUrl = $ga->getQRCodeGoogleUrl('luokakale', $secret, 'googleVerify');
  9. //echo $qrCodeUrl;

 

将上面生成的二维码地址放入网页img标签里面即可,示例图展示如下:

 

 接下来就是客户端谷歌验证APP扫码绑定后进行输码验证,验证PHP代码示例:

  1. //引入谷歌验证器类
  2. vendor('googleAuth.GoogleAuthenticator-master.PHPGangsta.GoogleAuthenticator');
  3. $ga = new \PHPGangsta_GoogleAuthenticator();
  4. //下面为验证参数
  5. $code = $_GET['code'];//客户提交上来的谷歌验证APP里面对应的验证码
  6. //该用户绑定谷歌验证生成的唯一秘钥
  7. $secret = 'VO2WA6NG3XZZEU4E';
  8. //验证用户提交的验证码是否正确
  9. $checkResult = $ga->verifyCode($secret, $code, 1);
  10. if ($checkResult) {
  11. echo 'SUCCESS';
  12. } else {
  13. echo 'FAILED';
  14. }

验证成功即客户绑定谷歌验证成功。该谷歌验证可用于客户登陆支付个人设置等各种场景。

 

本文属原创内容,为了尊重他人劳动,转载请注明本文地址:

 

https://www.cnblogs.com/luokakale/p/11384838.html

 

原文链接:http://www.cnblogs.com/luokakale/p/11384838.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号