简单的 PHP 身份验证循环


Simple PHP Authentication Looping

嗨:我正在尝试使用 $_SESSION 在 PHP 网页上实现身份验证;但是,它让我循环回身份验证页面。有人知道为什么吗?我使用的代码如下:

在需要身份验证的页面上:

<?php
session_start();
if(empty($_SESSION['authenticated']) || $_SESSION['authenticated'] != 'true') {
    header('Location: duo.php');
}
?>

登录/身份验证页面:

<?php
session_start();
include('duo_web.php');
echo "<center><h2>This site requires authentication.</h2>";
echo "<br><hr>";
unset($_SESSION['authenticated']);
if(isset($_POST['sig_response'])){
        $resp = Duo::verifyResponse(get_cfg_var('duo_ikey'), get_cfg_var('duo_skey'), get_cfg_var('duo_akey'), $_POST['sig_response']);
        if($resp != NULL){
                $_SESSION['authenticated'] = 'true'; header('Location: index.php');
        }
}
else if(isset($_POST['user']) && isset($_POST['pass'])){
        if($_POST['user'] == get_cfg_var('duo_user') && $_POST['pass'] == get_cfg_var('duo_pass')) {
                $sig_request = Duo::signRequest(get_cfg_var('duo_ikey'), get_cfg_var('duo_skey'), get_cfg_var('duo_akey'), $_POST['user']);
?>
                <script src="Duo-Web-v1.bundled.min.js"></script>
                <input type="hidden" id="duo_host" value="<?php echo get_cfg_var('duo_host') ; ?>">
                <input type="hidden" id="duo_sig_request" value="<?php echo $sig_request; ?>">
                <script src="Duo-Init.js"></script>
                <iframe id="duo_iframe" width="620" height="500" frameborder="0" allowtransparency="true" style="background: transparent;"></iframe>
<?php
        }
}
else {
        echo "<form action='duo.php' method='post'>";
        echo "Username: <input type='text' name='user' /> <br />";
        echo "Password: <input type='password' name='pass' /> <br />";
        echo "<input type='submit' value='Submit' />";
        echo "</form>";
}
?>

编辑:我尝试将我的实际内容移动到back.index.php并仅将上面的代码放入其中,但我仍然得到循环,因此问题必须出在我的登录(duo.php(页面中。我已经添加了上面页面中的所有代码。

在需要身份验证的页面上,session_start(( 需要在开始的 php 标签之后。

<?php
session_start();
if(empty($_SESSION['authenticated']) || $_SESSION['authenticated'] != 'true') {
    header('Location: duo.php');
}

嗨:所以这对我有用。问题在于变量未保存到 $_SESSION。我不得不将其移动到页面中的其他位置,并将 ' 替换为":

在需要授权的页面上:

<?php
ini_set("session.cookie_lifetime", "0");
ini_set("session.gc_maxlifetime", "3600");
session_start();
$var = $_SESSION["authenticated"];
if(strcmp($var,'yes') !== 0){
        header('Location: duo.php');
}
?>

在登录页面上:

<?php
ini_set("session.cookie_lifetime", "0");
ini_set("session.gc_maxlifetime", "3600");
session_start();
include('duo_web.php');
echo "<center><h2>This site requires authentication.</h2>";
echo "<br><hr>";
if(isset($_POST['sig_response'])){
        $resp = Duo::verifyResponse(get_cfg_var('duo_ikey'), get_cfg_var('duo_skey'), get_cfg_var('duo_akey'), $_POST['sig_response']);
        if($resp != NULL){
                header('Location: index.php');
        }
}
else if(isset($_POST['user']) && isset($_POST['pass'])){
        if($_POST['user'] == get_cfg_var('duo_user') && $_POST['pass'] == get_cfg_var('duo_pass')) {
                $sig_request = Duo::signRequest(get_cfg_var('duo_ikey'), get_cfg_var('duo_skey'), get_cfg_var('duo_akey'), $_POST['user']);
?>
                <script src="Duo-Web-v1.bundled.min.js"></script>
                <input type="hidden" id="duo_host" value="<?php echo get_cfg_var('duo_host') ; ?>">
                <input type="hidden" id="duo_sig_request" value="<?php $_SESSION["authenticated"] = "yes"; echo $sig_request; ?>">
                <script src="Duo-Init.js"></script>
                <iframe id="duo_iframe" width="620" height="500" frameborder="0" allowtransparency="true" style="background: transparent;"></iframe>
<?php
        }
}
else {
        echo "<form action='duo.php' method='post'>";
        echo "Username: <input type='text' name='user' /> <br />";
        echo "Password: <input type='password' name='pass' /> <br />";
        echo "<input type='submit' value='Submit' />";
        echo "</form>";
}
?>

希望这对其他人有所帮助。