网站的管理页面只有管理员才能访问,如果有人试图访问它,它会重定向到登录页面

admin page of website only to be accessed by admin if someone tries to access it , it redirects to login-page

这是我写的代码

if(!isset($_SESSION['authenticated'])){header("location:login.php");}

但此代码无效,正在访问中

你能把变量设置为 true 还是 false?

if ($_SESSION['authenticated']) {
   // Admin page here...
} else {
   // Redirect to another page...
   header("Location: login.php");
   die();
}

编辑:

我说将其设置为 true 或 false 的原因是您可以通过将其设置为 false 来注销用户。而不是取消设置变量。

编辑:

  • 删除了 = true,因为它不是必需的。

您在 header 函数中漏掉了大写字母 L,您需要在重定向后关闭该页面。

替换

header('location:index.php');

header("Location: login.php");
die();