我使用https://github.com/chregu/GoogleAuthenticator.php为正在开发的网络应用建立了两步验证。所有生成密钥和验证码的操作都正常。
现在我在另一台服务器上设置了相同的代码,生成了新的密钥,并将其添加到 Google Authenticator 移动应用程序中。但是移动应用程序生成的代码与之前不符。
通过比较这两个服务器的结果,我发现 time() 函数返回的时间不同(相差 1 小时),然后我强制我的第二个服务器(Google 代码不起作用的那台)与第一个服务器具有相同的时间,它就可以了。所以我真的很困惑,这是否是某种时区问题?因为我希望这些服务器有自己的时区。
有没有什么解决方法?
另外,我按照https://support.google.com/accounts/answer/185834?hl=en的说明同步了我的 Google Authenticator 应用程序,但仍然不起作用。移动应用程序生成的代码在一个小时后才能在我的第二个服务器上使用。请问有人能帮助我或建议我不同的方法吗?
这里是我用于连接上述库的代码:
class googleAuthLibrary extends GoogleAuthenticator
{
public function getSecretKey()
{
$secretKey = $this->generateSecret();
return $secretKey;
}
public function getQRLink($username, $hostname, $secretKey)
{
$url = 'https://chart.googleapis.com/chart?cht=qr&chs=300x300&chl=';
$qrCode = 'otpauth://totp/'.$username.'@'.$hostname.'?secret='.$secretKey;
$url = $url.$qrCode;
return $url;
}
public function getAuthCode($secretKey)
{
$authCode =$this->getCode($secretKey);
return $authCode;
}
}