【问题标题】:user can access admin page using session in php用户可以使用 php 中的会话访问管理页面
【发布时间】:2015-10-17 21:18:13
【问题描述】:

我有一个包含表用户的数据库。在用户表上,我有一个值为“corny”和 username="admin" 的用户名。

在我的 user.php 上我有这个代码

if(!isset($_SESSION['username'])){
    header('location: index.php');
}

在我的 admin.php 上我有这个会话

if(!isset($_SESSION['username'])){
    if($_SESSION['username'] != "gft-admin"){
        header('location: index.php');
    }
}

所以我的问题是我可以从 users.php 访问 admin.php。如果用户不等于'admin',我应该怎么做才能避免users.php访问admin.php。

【问题讨论】:

    标签: php mysql session


    【解决方案1】:

    您当前的逻辑是:

    IF there is no username
      THEN if the username is not gft-admin
        THEN REDIRECT
    

    但你想说的是:

    IF there is no username
    OR if the username is not gft-admin
        THEN redirect
    

    你想表达什么:

    if(!isset($_SESSION['username']) || $_SESSION['username'] != "gft-admin"){
        header('location: index.php');
    }
    

    【讨论】:

      【解决方案2】:

      这是因为当会话已经设置时,您完全绕过了您的管理员 if 阻止。我认为您只需要删除! 标记即可。

      【讨论】:

      • 当我尝试浏览器 admin.php 时,我仍然可以访问
      • @akirayuki 尝试先清除您的浏览器/会话缓存。
      【解决方案3】:

      删除第二段代码中的!,并在没有设置$_SESSION['username']时添加else语句。

      if(isset($_SESSION['username'])){ //remove here
          if($_SESSION['username'] != "gft-admin"){
              header('location: index.php');
          }
      } else {
          header('location: index.php');
      }
      

      【讨论】:

      • 当我尝试访问浏览器 admin.php 时仍然可以访问
      • @akirayuki 更新了答案,用 else 语句再试一次。
      猜你喜欢
      • 1970-01-01
      • 2021-10-17
      • 1970-01-01
      • 2014-02-16
      • 2016-05-09
      • 2020-09-01
      • 2021-01-07
      • 2016-09-19
      • 1970-01-01
      相关资源
      最近更新 更多