简单的PHP身份验证循环

时间:2014-12-21 03:23:55

标签: php html session authentication web

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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

嗨:这对我有用。问题是变量没有保存到$ _SESSION。我不得不将它移动到页面中的其他位置,然后我将其替换为“with”:

在需要授权的页面上:

<?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>";
}
?>

希望这有助于其他人。