会话可以参与包含功能吗?



我有3个文件:index.php/header.php和footer.php

我希望会话在标题之前.php

索引.php

<?php include("header.php"); ?>
// Some Data inside index.php
<?php include("footer.php"); ?>

标头.php

<?php if(session('access_token')) { ?>

页脚.php

<?php } ?>

如何使此方法有效? 我需要会话从标题开始.php但不想在那里关闭他!

您可以在任何文件中设置 PHP 会话,只要您始终启动它们,因此请确保在所有要保护的文件中包含header.php

您的标头.php应包括:

<?php
//Start PHP session if not already started
if(session_id() == '') {
session_start();
}
?>

你的索引,php文件可能包含这样的东西:

<?php
/* Include header */
require("header.php");
if(isset($_SESSION["access_token"])){
/* Content only if the access token is in session */
} else {
die("Access token not found");
}
/* Include footer */
include("footer.php");
?>

至于你的页脚.php,这不应该影响上述任何PHP代码或改变你想要实现的结果。

我认为你不能再做一个php文件。 但我有解决方案 第一 使 PHP 文件包含所有页眉、索引和页脚

那你可以这样做

if(isset($_SESSION['access_token'])) {
include("header.php"); 
include("index.php");
include("footer.php");
} else {
die; // or you can use header('Location: http://www.example.com/'); 
}

最新更新