**使用说明:**开启Google的登陆二步验证(即Google Authenticator服务)后用户登陆时需要输入额外由手机客户端生成的一次性密码。实现Google Authenticator功能需要服务器端和客户端的支持。服务器端负责密钥的生成、验证一次性密码是否正确。客户端记录密钥后生成一次性密码。
下载谷歌验证类库文件放到项目合适位置(我这边放在项目Vender下面)
下载地址: https://github.com/PHPGangsta/GoogleAuthenticator
//引入谷歌验证器类
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标签里面即可,示例图展示如下:
<?php
require_once 'PHPGangsta/GoogleAuthenticator.php';
$ga = new PHPGangsta_GoogleAuthenticator();
$secret = $ga->createSecret();
echo "Secret is: ".$secret."\n\n";
$qrCodeUrl = $ga->getQRCodeGoogleUrl('Blog', $secret);
echo "Google Charts URL for the QR-Code: ".$qrCodeUrl."\n\n";
$oneCode = $ga->getCode($secret);
echo "Checking Code '$oneCode' and Secret '$secret':\n";
$checkResult = $ga->verifyCode($secret, $oneCode, 2); // 2 = 2*30sec clock tolerance
if ($checkResult) {
echo 'OK';
} else {
echo 'FAILED';
}
