如何检查会话是否在header.php中设置

时间:2015-03-31 23:50:52

标签: php html

我有超过30页,其中20页只能由用户看到,所以我想检查用户是否使用if语句登录。

if($_SESSION['username']){
//Display Page (All HTML code is here.)
}else{
 header("Location: index.php");
}

问题是我不想为每个页面键入相同的内容。所以,我想我可以在header.php中输入这个部分

if($_SESSION['username']){

这部分在footer.php

}else{
 header("Location: index.php");
}

但它没有那样的工作。我想知道我该怎么办?什么是最有效的方式?

1 个答案:

答案 0 :(得分:1)

在档案中:
if(!$_session['username']){ header("location: index.php"); }

以及您需要的任何地方: require("myfile.php");

相关问题