您的位置 首页 php

php通过谷歌身份验证实现动态口令

Google Authenticator PHP类

生成安全码并绑定手机

<?php
​
require_once './PHPGangsta/GoogleAuthenticator.php';
​
$ga = new PHPGangsta_GoogleAuthenticator();
​
// 创建新的"安全密匙SecretKey"
// 把本次的"安全密匙SecretKey" 入库,和账户关系绑定,客户端也是绑定这同一个"安全密匙SecretKey"
// 安全密匙SecretKey 可以和手机端绑定
$secret = $ga->createSecret();
​
echo "安全密匙SecretKey: " . $secret . "\n\n";
​
//第一个参数是"标识",第二个参数为"安全密匙SecretKey" 生成二维码信息
$qrCodeUrl = $ga->getQRCodeGoogleUrl('www.yundou.com', $secret);
​
//Google Charts接口 生成的二维码图片,方便手机端扫描绑定安全密匙SecretKey
echo "Google Charts URL for the QR-Code: " . $qrCodeUrl . "\n\n";
​
 

输出:

安全密匙SecretKey: M5X3M4PGBQRFPUTY
​
Google Charts URL for the QR-Code: 
​
 

绑定手机方式(可以使用谷歌身份验证器或者FreeOTP)

  • 通过安全秘钥
  • 通过二维码(图片 地址 就是Google Charts生成的可以直接打开)

生成二维码样例地址(二维码是不让发):

<?php
​
require_once './PHPGangsta/GoogleAuthenticator.php';
​
$ga = new PHPGangsta_GoogleAuthenticator();
​
// 把提交的验证码和服务端上生成的验证码做对比
// $secret 服务端的 "安全密匙SecretKey"
// $oneCode 手机上看到的 "一次性验证码"
// 最后一个参数 为容差时间,这里是2 那么就是 2* 30 sec 一分钟.
$oneCode = '371922';
$secret = 'M5X3M4PGBQRFPUTY';
​
$checkResult = $ga->verifyCode($secret, $oneCode, 2);
​
​
if ($checkResult) {
 //这里添加自定义逻辑
 echo '匹配! OK';
} else {
 echo '匹配! FAILED';
}
​
 

文章来源:智云一二三科技

文章标题:php通过谷歌身份验证实现动态口令

文章地址:https://www.zhihuclub.com/153181.shtml

关于作者: 智云科技

热门文章

网站地图