【问题标题】:Session Variables not working in header even though session_start is present即使 session_start 存在,会话变量在标头中也不起作用
【发布时间】:2019-02-20 03:14:26
【问题描述】:

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

index.php

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

//some html stuff

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

header.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();iteself)能够获取会话变量。

我尝试移动session_start() 并使用ob_startob_flush,但没有找到任何方法可以在我的头文件中使用会话变量(特别是在导航栏中)。我做错了什么??

编辑

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

index.php

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

settings.php

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

header.php

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

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

附加编辑

来自 phpinfo() 的会话信息:

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

【问题讨论】:

    标签: php session session-variables


    【解决方案1】:

    在第 1 行的 header.php 中有

    会话开始();不是 session_start(); (已修复)

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

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

    好的,删除所有session_start();,然后在index.php中添加一个session_start();

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

    【讨论】:

    • 我不知道它是如何复制到 Stack Overflow 上的,但是我的头文件看起来像这样 &lt;?php session_start(); ?&gt; 我会在这里更新。
    • ...编辑@Anna
    • 一切都保持不变:(
    • @RADEK 这个编辑没有意义,使这个答案毫无价值。
    • @Martin 对此表示感谢和抱歉,Anna 有任何 PHP 错误吗?
    【解决方案2】:

    试试这个ob_start();

    ob_start();
    session_start();
    if(!isset($_SESSION['user_name'])){
    
    }
    

    【讨论】:

    • 为什么要试试这个?这有什么帮助?
    • 你是如何制作 $_SESSION['user_name'] 的可能有问题@Anna
    猜你喜欢
    • 2018-04-22
    • 1970-01-01
    • 2021-04-23
    • 2015-06-25
    • 2014-08-14
    • 1970-01-01
    • 2012-04-15
    • 2011-01-20
    相关资源
    最近更新 更多