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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度