使用说明:开启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';
}

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

 

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