简单的页面密码保护:我在这里做错了什么?

时间:2015-11-11 19:21:46

标签: php html session post

对于我的测试环境网站,我希望任何查看它的人都必须在每个会话中键入一次密码。当他们的会话开始时,他们输入密码并被重定向到他们试图查看的页面。所以我在每个页面的顶部

// For test site, require login
if ( !$_SESSION['loggedIn'] )
{
    $pagetarget = $_SERVER['REQUEST_URI'];
    include "test_login.php";
    die();
}

然后test.login.php

<?php 
    if ( $_POST['password'] == '091u233j12j3' )
    {  
       $_SESSION['loggedIn'] = true;
       header("Location: " . $pagetarget);
       die();
    }
?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Test Environment Login</title>
</head>
<body>
    <form method="POST">
        <p>Enter password to unlock <b>test.mysite.com</b>: <input type="text" name="password" /><input type="submit" /></p>
        <p>You will be redirected to the page you tried to access.</p>
        <input type="hidden" name="pagetarget" value="<?php echo $pagetarget; ?>" />
    </form>
</body>
</html>

但页面重定向不起作用。我究竟做错了什么?

2 个答案:

答案 0 :(得分:2)

在使用session_start()之前,您需要在每个脚本的开头调用$_SESSION

示例:

session_start();
if ( !$_SESSION['loggedIn'] )
{
    $pagetarget = $_SERVER['REQUEST_URI'];
    include "test_login.php";
    die();
}

答案 1 :(得分:0)

试试这个:

len(data)

// For test site, require login session_start(); if ( !$_SESSION['loggedIn'] ) { $pagetarget = $_SERVER['REQUEST_URI']; include "test_login.php"; die(); }

test_login.php