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


How to check if the session is set in header.php

我有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");
}

但它不是那样工作的。我想知道我能做什么?最有效的方法是什么?

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

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