简体   繁体   English

如何防止用户使用用户 session_destroy 访问管理页面

[英]How to prevent users to access admin page with user session_destroy

I want to prevent logged in users from accessing admin page with their session when they are logged in. they should only be able to log in only with username 'admin'.我想防止登录用户在登录时使用他们的 session 访问管理页面。他们应该只能使用用户名“admin”登录。

   session_start(); 

   if (!isset($_SESSION['username'])) {
      $_SESSION['msg'] = "You must log in first";
      header('location: login.php');
   }

   if (isset($_SESSION['username']) != 'admin') {
      session_destroy();
      unset($_SESSION['username']);
      header("location: login.php");
   }

   if (isset($_GET['logout'])) {
      session_destroy();
      unset($_SESSION['username']);
      header("location: login.php");
   }
if (isset($_SESSION['username'])) {
   if($_SESSION['username'] !== 'admin'){
     session_destroy();
     unset($_SESSION['username']);
     header("location: login.php");
   }

} else {
   $_SESSION['msg'] = "You must log in first";
   header('location: login.php');
}

the isset function would return a boolean value and must not be checked against string. isset function 将返回 boolean 值,不得根据字符串进行检查。

if (isset($_COOKIE[session_name()])) {
            setcookie(session_name(), '', time() - 86400, '/');
}
session_destroy();
header("location: login.php");
exit();

session_destroy() is not enough. session_destroy()是不够的。 you should expire cookie too.你也应该让 cookie 过期。

and always use exit();并始终使用exit(); after header('location=...');header('location=...');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM