PHP$\u会话在包含的页面上工作吗?

PHP$\u会话在包含的页面上工作吗?,php,session,Php,Session,假设我有header.tpl和footer.tpl,在这两者之间是body.tpl 比方说,我想通过检查特定会话是否设置为如下方式来限制对body.tpl的访问: session_start(); if (isset($_SESSION['limited'])) { //render body.tpl } else { //direct somewhere else } 如果session_start(),这会起作用吗;如果(isset()){部分位于header.tpl中,而}其他{

假设我有header.tpl和footer.tpl,在这两者之间是body.tpl

比方说,我想通过检查特定会话是否设置为如下方式来限制对body.tpl的访问:

session_start();
if (isset($_SESSION['limited'])) {
  //render body.tpl
} else {
  //direct somewhere else
}
如果
session_start(),这会起作用吗;如果(isset()){
部分位于header.tpl中,而
}其他{}
位于footer.tpl文件中

我的计划是,我在每个页面的页眉和页脚中进行会话检查,这样我就不必在呈现模板的PHP文件中进行检查

这样(body.tpl的内容):


//body.tpl内容

通过一个PHP文件调用body.tpl,该文件
需要所有必要的文件,并实例化将用于填充tpl文件的所有类。如果我在PHP文件中执行会话检查,它将按预期工作。

您可以在执行请求时从任何PHP代码访问$\u会话对象。请确保只调用一次会话启动


注:这种方法会带来代码重复,你会后悔的。

如果你使用模板引擎,你会做得更好(试试看)

你将能够轻松地完成这项工作,甚至更多。你将能够按照这些思路做一些事情

假设您想要显示一个foo.tpl页面,并且您还有两个其他smarty模板 1.header.tpl 2.footer.tpl

tpl看起来像这样

{include file="header.tpl"}

{$foo}

{include file="footer.tpl"}
然后在php页面中,执行您想要的任何逻辑,通过

$smarty-assign('foo',$my_content);
$smarty->display( "foo.tpl" );
这将呈现foo.tpl,包括header.tpl和footer.tpl以及$foo的内容

$smarty-assign('foo',$my_content);
$smarty->display( "foo.tpl" );