【发布时间】: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_start 和ob_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