我不想在所有文件中逐个控制会话,而是想控制header.php
每个文件中包含的会话。所以,在header.php
我编码的顶部:
if($session->is_logged_in()) {//is_logged_in is a function controlling session
echo "logged";
} else {
echo "not logged";
}
index.php 这里:
<?php define('REALLY_INCLUDED', true);?>
<?php require_once $_SERVER['DOCUMENT_ROOT'].'/includes/initialize.php'; ?>
<?php include_layout_template("index_header.php"); ?>
<?php include_layout_template("sidebar.php"); ?>
<?php include_layout_template("index_content.php"); ?>
<?php include_layout_template("footer.php");?>
index_header.php 在这里:
<?php require_once $_SERVER['DOCUMENT_ROOT'].'/includes/initialize.php'; ?>
<?php header("Content-Type: text/html; charset=latin5"); ?>
<?php
if(!defined('REALLY_INCLUDED') || !REALLY_INCLUDED) {
exit();
}
if($session->is_logged_in()) {
echo "logged in";
} else {
echo "logged not!";
}
?>
<html>
Here is html code
</html>
session.php 的某些部分:
private $logged_in=false;
function __construct() {
session_start();
$this->check_login();
}
public function is_logged_in() {
return $this->logged_in;
}
private function check_login() {
if(isset($_SESSION['user_id'])) {
$this->user_id = $_SESSION['user_id'];
$this->logged_in = true;
} else {
unset($this->user_id);
$this->logged_in = false;
}
}
问题是,当我单击 URL index.php 时,浏览器中没有任何内容。如果,我删除该is_logged_in()
功能,它会显示页面。如果我将is_logged_in()
函数放入 index.php 并从 index_header.php 中删除,它也会显示页面。index_header.php
如果,我单击它显示的 URL 。有趣的是,这个函数不包含 index_header.php。我的功能没有问题。