首页 > 解决方案 > 即使 session_start 存在,会话变量在标头中也不起作用

问题描述

我已经在网络(包括 Stack Overflow)上搜索了正在发生的事情的答案,但没有运气:( 我正在尝试建立一个由会话完成的基于用户的网站。我的网站是这样设置的:

索引.php

<?php include("header.php");

//some html stuff

echo "<pre>";
print_r($_SESSION);
echo "</pre>"; 
?>

头文件.php

<!DOCTYPE html>
<html>
<head>
    <title>My title</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
    <link rel="stylesheet" href="style.css" />
    <link rel='shortcut icon' href='images/favicon.ico' type='image/x-icon'/ >
    <?php
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);

    date_default_timezone_set('America/Denver');
    ?>
</head>
<body>
    <div class="header">
        <div class='outer-nav'>
            <ul>
                <?php //navigation
                if (isset($_SESSION['user_name'])) {
                    echo '<li>' . $_SESSION['user_name'] . '</li>';
                } else {
                    echo '<li>user_name does not exist</li>';
                    echo "<pre>";
                    print_r($_SESSION);
                    echo "</pre>";
                }
                ?>
            </ul>

问题是即使session_start();在头文件中,头文件也不会识别会话变量,但是包含头文件的索引文件(并且没有单独的session_start();自身)能够获取会话变量。

我尝试过移动session_start()和使用ob_startand ob_flush,但还没有找到任何方法来获取要在我的头文件中使用的会话变量(特别是在导航栏中)。我究竟做错了什么??

怎么了

编辑

好的,所以我想我知道问题出在哪里。我的索引在标题之前有一个包含:

索引.php

<?php include("admin/settings.php"); 
include("header.php");

设置.php

<?php session_start(); ?>
//just variable declarations here

头文件.php

//removed <?php session_start(); ?>
//see above for rest of code in header.php

当我将代码更改为此时,现在我在索引正文中获取会话变量,但现在我进入Notice: Undefined variable _SESSION in...标题。所以现在它甚至根本看不到会话......这是因为它是一个包含吗?

附加编辑

我来自 phpinfo() 的会话信息:

会话信息

login.php(声明会话变量的地方):

<?php

include_once("admin/settings.php");

$inputusername = mysqli_real_escape_string($connect, $_POST['user_name']);
$inputpassword = mysqli_real_escape_string($connect, $_POST['user_pass']);

$sql = "SELECT user_id, user_name, user_pass, user_role, user_lastloggedin
    FROM user WHERE user_name = '$inputusername'";

$result = mysqli_query($connect, $sql);

if (!$result) { //query returned a mistake
    include_once($header);
    echo '<div class="full_page_content">';
    echo "<h1>Sign In</h1>";
    echo '<center>Something went wrong while signing in. Please try again later.</center>';
    //echo mysqli_error();
    include_once($footer);
    exit();
} elseif (mysqli_num_rows($result) == 0) { //check if user exists
    include_once($header);
    echo '<div class="full_page_content">';
    echo "<h1>Sign In</h1>";
    echo '<center>That username does not exist. Please check your spelling and try again.</center>';
    include_once($footer);
    exit();
} else { //user was found, continue login
    while ($row = mysqli_fetch_assoc($result)) {
        $name = $row['user_name'];
        $pass = $row['user_pass'];
        $id = $row['user_id'];
        $role = $row['user_role'];
        $last = $row['user_lastloggedin'];
    }

    $errors = array();
    if (empty($inputpassword)) { //check if password field is empty
        $errors[] = '<center>The password field must not be empty</center>';
    } elseif (!password_verify($inputpassword, $pass)) {
        $errors[] = '<center>The password you entered was incorrect. Please check your spelling and try again.</center>';
    }


    if (!empty($errors)) { //if there are errors...
        include_once($header);
        echo '<div class="full_page_content">';
        echo "<h1>Sign In</h1>";
        echo '<ul>';
        foreach ($errors as $key => $value) {
            echo '<li>' . $value . '</li>';
        }
        echo '</ul>';
        include_once($footer);
        exit();
    } else {
        //there are no errors, process the form
        $_SESSION['signed_in'] = true;
        $_SESSION['user_id'] = $id;
        $_SESSION['user_name'] = $name;
        $_SESSION['user_role'] = $role;
        $now = time();

        if (strlen($last) == 1) {
            $_SESSION['first'] = true;
            header("Location: changepass.php");
        } else {
            mysqli_query($connect, "UPDATE users SET user_lastloggedin='$now' WHERE user_id = '$id'");
            header("Location: index.php");
        }
    }
}
?>

标签: phpsessionsession-variables

解决方案


在第 1 行的 header.php 中,您有

会话开始();不是 session_start(); (固定的)

此外,您似乎还忘记关闭第 1 行的 PHP 标记。

如果在之前回显会话变量session_start();会发生什么,如果在之后回显它会发生什么(在任何 if 语句之外)?

好吧,删除 all ,然后在 index.php 中session_start();添加一个。session_start();

session_start();确保在您包含的所有文件中只有 1 个。


推荐阅读