PHP 为项目设置谷歌身份验证器
运行之后是这样的,会生成一个二维码图片的地址,使用手机下载身份验证器app后扫此二维码绑定关系。下面的可以认为是一个生成验证器的方法。其实只需要其中一个类就够了。
·
其实只需要其中一个类就够了
下面的可以认为是一个生成验证器的方法
<?php
/**
* 创建验证器
*/
require_once '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';
}
运行之后是这样的,会生成一个二维码图片的地址,使用手机下载身份验证器app后扫此二维码绑定关系

验证动态码
<?php
/**
* 模拟验证
*/
require_once 'GoogleAuthenticator.php';
$ga = new PHPGangsta_GoogleAuthenticator();
//下面为验证参数
$code = '178467';//客户提交上来的谷歌验证APP里面对应的验证码
//该用户绑定谷歌验证生成的唯一秘钥
$secret = 'Q5EGCPFSEY54KX2S';
//验证用户提交的验证码是否正确
$checkResult = $ga->verifyCode($secret, $code, 1);
if ($checkResult) {
echo 'SUCCESS';
} else {
echo 'FAILED';
}
更多推荐
所有评论(0)