dqab0824
dqab0824
2019-03-28 06:53

服务器上的时间不匹配 - php中的双因素身份验证

已采纳

I've created two-factor authentication in PHP using google authenticator. It works well on my localhost, but on my website the generated code was mismatched. I found this is a time configuration issue. assist me to set up time on two-factor.

public function verifyCode($secret, $code, $discrepancy = 1, $currentTimeSlice = null)
{
    if ($currentTimeSlice === null) {
        $currentTimeSlice = floor(time() / 30);
    }

    for ($i = -$discrepancy; $i <= $discrepancy; $i++) {
        $calculatedCode = $this->getCode($secret, $currentTimeSlice + $i);
        if ($calculatedCode == $code ) {
            return true;
        }
    }

    return false;
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • duandai0373 duandai0373 2年前

    Most likely it is a time drift on the web server. Make sure the time on the web server is correct and precise (UTC).

    点赞 评论 复制链接分享