PHP设置谷歌验证器(Google Authenticator)实现操作二步验证

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

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

https://github.com/PHPGangsta/GoogleAuthenticator

PHP代码示例:

//引入谷歌验证器类
vendor('googleAuth.GoogleAuthenticator-master.PHPGangsta.GoogleAuthenticator');
$ga = new \PHPGangsta_GoogleAuthenticator();

//这是生成的密钥,每个用户唯一一个,为用户保存起来用于验证
$secret = $ga->createSecret();
//echo $secret;

//下面为生成二维码,内容是一个URI地址(otpauth://totp/账号?secret=密钥&issuer=标题)
$qrCodeUrl = $ga->getQRCodeGoogleUrl('luokakale', $secret, 'googleVerify');
//echo  $qrCodeUrl;

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

 

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

//引入谷歌验证器类
vendor('googleAuth.GoogleAuthenticator-master.PHPGangsta.GoogleAuthenticator');
$ga = new \PHPGangsta_GoogleAuthenticator();

//下面为验证参数
$code = $_GET['code'];//客户提交上来的谷歌验证APP里面对应的验证码

//该用户绑定谷歌验证生成的唯一秘钥
$secret = 'VO2WA6NG3XZZEU4E';

//验证用户提交的验证码是否正确
$checkResult = $ga->verifyCode($secret, $code, 1); 

if ($checkResult) {
    echo 'SUCCESS';
} else {
    echo 'FAILED';
}

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

转自:PHP设置谷歌验证器(Google Authenticator)实现操作二步验证

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值