如何为我的网站实施Google身份验证?

时间:2016-02-03 12:58:58

标签: php google-authentication google-authenticator

我一直在尝试为我的网站实施最长时间的Google身份验证,但编程确实不是我最强的诉讼。以下是我的编码,如果有人愿意指出它为什么不能正常工作,我将非常感激。目前,我能够动态创建可以使用Google身份验证应用程序扫描的QR码,该应用程序将生成一次性代码。然而,问题是,当我输入代码时,无论我是否输入了正确的代码,它总是会说我错了。请耐心等待我,就像我说的那样,我对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>

0 个答案:

没有答案