我认为您的问题是 fubar 换行符 :) 添加了登录页面参考,如果登录失败,则取消设置您检查的会话变量。
非常小心放置<?php ?>
标签的位置 - 以及在 .inc 文件中,例如 db.php 包含 - 您可以省略?>
最后一行的结束标签以避免意外 ENTER,然后 CTRL + S 失败,潜入不需要的换行符在您的输出缓冲区(ob)中。如果 ob_start 被激活,则在您选择或脚本结束之前不会从服务器写入任何内容。否则,如果不是,则默认为每个 \n 将刷新输出并启动有效负载的内容部分。
登录.php:
<?php
session_start(); // put this on top-most line in your script
$ok = check($_POST['user'], $_POST['pass']);
if($ok) {
$user = db_get_user_creds($_POST['user']);
$_SESSION['name'] = $user['name'];
$_SESSION['job_title'] = $user['job_title'];
} else {
// session_unset();
unset($_SESSION['name']);
unset($_SESSION['job_title']);
}
?>
管理员.php
<?php
session_start(); // put this on top-most line in your script
// or, use ob_start at the very first line
// (with no widespace what so ever written out before it)
include('db.php');
?> I am writing out a newline here, session / header section is going to become unstable
<?php
// Inialize session
// @session_start(); moved up top
ob_start();
// Check, if username session is NOT set then this page will jump to login page
if (!isset($_SESSION['name']) || ($_SESSION['job_title'] != "admin")) {
header('Location: index.php');
}
ob_flush();
?>
请参阅“我在这里写出换行符”位
更深一点的兔子洞去;通信流程是这样的:
1) HEADERS such as
Connection: keep-alive\r\n
Content-Type: text/html\r\n (etc)
2) DOUBLE NEWLINE (one newline with no previous chars on that line)
\r\n
3) CONTENTS
Body
Of Page