很长时间以来,我一直在尝试为我的网站实施 Google 身份验证,但编程确实不是我的强项。以下是我的编码,如果有人能指出为什么它不能正常工作,我将不胜感激。目前,我能够动态创建一个可以使用 Google 身份验证应用程序扫描的二维码,该应用程序将生成一次性代码。然而,问题是,当我输入代码时,它总是说我错了,不管我输入的代码是否正确。请耐心等待我,正如我所说,我不太擅长 PHP。
<?php
session_start();
require 'GoogleAuthenticator.php';
$ga = new PHPGangsta_GoogleAuthenticator();
$secret = $ga->createSecret();
echo 'Secret:'.$secret; //echo-ed the secret so it would be easier for you guys
$qrCodeUrl = $ga->getQRCodeGoogleUrl('Three Musketeers', $secret);
$oneCode = $ga->getCode($secret);
echo 'One-Code:'.$oneCode; //echo-ed the oneCode so you guys wouldn't need to scan the QR Code everytime
$_SESSION['oneCode'] = $oneCode;
?>
<br>
<br>
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<img src="<?php echo ''.$qrCodeUrl;?>" />
<form action="2fa.php" method="POST">
Code: <input type="text" name="code" maxlength="6"><br>
<input type="submit" name="submit" value="submit">
</form>
<?php
if(isset($_POST['submit']))
{
$code = $_POST["code"];
if (empty($_POST['code']))
{
echo "<script>alert('EMPTY');</script>";
}
else
{
if ($code == $_SESSION['oneCode']) //I tried just putting $code == $oneCode, but it doesn't work as well. However, you guys may feel free to try anyway
{
echo "<script>alert('CORRECT');</script>";
}
else
{
echo "<script>alert('WRONG');</script>";
}
}
}
?>
</body>
</html>