PHP SESSION数组在从一个文件夹重定向到另一个文件夹时被删除


PHP SESSION array getting deleted while redirecting from one folder to another

好吧,我不知道我犯了什么错误。我过去也使用过SESSION变量,没有任何问题,但这次似乎不起作用。

这是我的protected_page.php(登录成功后重定向用户的页面)中的代码。

<?php if (login_check($mysqli) == true) : ?>
        <p>Welcome <?php echo htmlentities($_SESSION['username']); ?>!</p>
        <p>
            This is an example protected page.  To access this page, users
            must be logged in.  At some stage, we'll also check the role of
            the user, so pages will be able to determine the type of user
            authorised to access the page.
            </br></br>
            <a href="add/home.php">Add new user!</a>
        </p>
        <p>Logout & return to <a href="includes/logout.php">login page</a></p>
    <?php else : ?>
        <p>
            <span class="error">You are not authorized to access this page.</span> Please <a href="includes/logout.php">login</a>.
        </p>
    <?php endif; ?>

函数login_check()检查用户是否使用正确的凭据正确登录。如果是,则返回true。

还有另一个函数,即login(),它在登录过程中被调用,并将凭据与输入的凭据进行匹配。如果为true,则将"username"存储在$_SESSION变量中。

当我点击"添加新用户!"时,它会将我发送到Add/home.php页面,但没有$_SESSION变量。

if (login_check($mysqli) == true) : ?>
    <p>Welcome <?php echo htmlentities($_SESSION['username']); ?>!</p>
    <h1>Add user!</h1>
    <?php
    if (!empty($error_msg)) {
        echo $error_msg;
    }
    ?>
    <ul>
        <li>Emails must have a valid email format</li>
    </ul>
    <form action="<?php echo esc_url($_SERVER['PHP_SELF']); ?>" 
            method="post" 
            name="registration_form">
        Full Name: <input type='text' 
            name='fullname' 
            id='fullname' /><br>
        Email: <input type="text" name="email" id="email" /><br>
        Project Name: <input type="text"
                         name="project" 
                         id="project"/><br>
        Phone Number: <input type="text" 
                                 name="phone" 
                                 id="phone" /><br>
        <input type="submit" 
               value="Submit"  /> 
    </form>
    <p>Logout & return to <a href="../includes/logout.php">login page</a></p>
    <?php else : echo "7"; ?>
        <p>
            <span class="error">You are not authorized to access this page.</span> Please <a href="../includes/logout.php">login</a>.
        </p>
    <?php endif; ?>

它总是打印我没有足够的凭据来查看此页面。

我还在这两个页面上介绍了sec_session_start()函数。(protected_page.php&add/home.php).sec_session_start()是用户定义的自定义会话启动函数。

function sec_session_start() {
$session_name = 'sec_session_id';   // Set a custom session name
$secure = SECURE;
// This stops JavaScript being able to access the session id.
$httponly = true;
// Forces sessions to only use cookies.
if (ini_set('session.use_only_cookies', 1) === FALSE) {
    header("Location: ../error.php?err=Could not initiate a safe session (ini_set)");
    exit();
}
// Gets current cookies params.
$cookieParams = session_get_cookie_params();
session_set_cookie_params($cookieParams["lifetime"],
    $cookieParams["path"], 
    $cookieParams["domain"], 
    $secure,
    $httponly);
// Sets the session name to the one set above.
session_name($session_name);
session_start();            // Start the PHP session 
session_regenerate_id();    // regenerated the session, delete the old one. 

}

我不知道这次我做错了什么。一定是个愚蠢的错误。提前感谢。:)

如需更多详细信息,请通知我。

您需要在页面顶部的所有.php页面上都有session_start()。

确保你有它。即使你在函数中有它,它也可能会把它放得太底。

你有这样的事情:

session_name($session_name);
session_start();            // Start the PHP session 
session_regenerate_id();    // regenerated the session, delete the old one. 

您需要将session_start作为页面上的第一件事。确保该功能离页面顶部不远。