即使存在session_start,会话变量也无法在标头中使用

时间:2018-09-15 16:31:58

标签: php session session-variables

我已经在网络上搜寻了发生的问题(包括堆栈溢出),但是没有运气:(我试图建立一个由会话完成的基于用户的网站。我的网站已设置像这样:

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,但是没有找到任何方法能够获取要在我的头文件中使用的会话变量(在导航栏)。我在做什么错??

what is happening

编辑

好,所以我想我已经找出问题的根源。我的索引在标头之前有一个include:

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()获得的会话信息:

session info

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

2 个答案:

答案 0 :(得分:0)

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

会话start();不是session_start(); (已修复)

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

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

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

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

答案 1 :(得分:-3)

改为尝试ob_start();

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

}