【问题标题】:Simple PHP Authentication Looping简单的 PHP 身份验证循环
【发布时间】:2014-12-21 03:23:54
【问题描述】:

嗨:我正在尝试使用 $_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) 页面中。我已经添加了上面页面中的所有代码。

【问题讨论】:

    标签: php html session authentication web


    【解决方案1】:

    在需要认证的页面上,session_start() 需要在开始的php标签之后。

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

    【讨论】:

    • 是的;当我添加代码标签时,我的错字。有问题已更正。
    【解决方案2】:

    嗨:所以这对我有用。问题在于未将变量保存到 $_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>";
    }
    ?>
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-29
      • 1970-01-01
      • 2010-10-07
      • 1970-01-01
      • 2012-08-06
      • 2017-11-12
      相关资源
      最近更新 更多