dongxing7083 2017-06-20 16:19
浏览 106
已采纳

在PHP中管理两个不同的SESSION

I have a table for users in my MySQL database with a tinyint value (0 or 1) which I use to determinate the category of the user.

So, at my login.php, I get the value (stored as 'admin'):

$query = $db->query("SELECT ..., admin FROM users WHERE email='$mail'"); $row = $query->fetch_array(); $isadmin = intval($row['admin']);

Then I assign the session:

  if (password_verify($pwd, $row['password']) && $count==1){
      if($isadmin==1) {
        $_SESSION['admin_session'] = $row['userid'];
        header("location: adminpanel.php");
      } else {
        $_SESSION['user_session'] = $row['userid'];
        header("location: adminpanel.php");
      }
  }

And when it comes to check the session, I do this:

if (isset($_SESSION['user_session'])){
    header("location: adminpanel.php");
    exit;
} else if(isset($_SESSION['admin_session'])){
    header("location: adminpanel.php");
    exit;
}

But... It's not working. The page doesn't load and it shows a browser error message saying there are too many redirections being made. How can I do this?

I know both sessions are heading to the same "adminpanel.php". What I'm trying to do is both can access but once they're logged, depending on its category (whether they're admin or not), they'll be able to do certain stuff.

  • 写回答

2条回答 默认 最新

  • dsnnvpobaljihv3490 2017-06-20 16:30
    关注

    I would suggest simplifying the process and just keeping a User in the session with a flag telling you if they are an admin or not.

    $query = $db->query("SELECT ..., admin FROM users WHERE email='$mail'");
    $row = $query->fetch_array();
    
    if (password_verify($pwd, $row['password'])){
        $_SESSION['user'] = $row['userid'];
        $_SESSION['isadmin'] = $row['admin'] == 1 ? true : false;
    
    }
    

    And when it comes to check the session, I do this:

    if (isset($_SESSION['isadmin']) && $_SESSION['isadmin']){
        header("location: adminpanel.php");
        exit;
    } else 
        // NOTE you had this redirecting exactly as above to adminpanel
        header("location: userpanel.php");
        exit;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器