简体   繁体   English

登录后如何根据用户角色将用户重定向到所需页面?

[英]How to redirect user after login to a desired page according to their role?

I have 2 roles called Member and Admin. 我有2个角色,分别称为成员和管理员。 However, as for now to distinguish between sessions, I only did an if else statements. 但是,至于现在要区分会话,我只做了if else语句。 Like this, 像这样,

session_start();
$msg = "";


if (isset($_SESSION['user_id'])) {
 ###############################
} else { 
 ################################

if (isset($_POST['username'])) {
 #################################
   <?php
    if (isset($_SESSION['user_id'])) {
        if ($_SESSION['role'] == "Admin") {
            ?>
          ################################
          ################################
   <?php
        } elseif ($_SESSION['role'] == "Member") {
            ?>
          #################################
          #################################

The following hash signs are just putting in what a member should see and what an admin should see. 以下哈希符号仅用于说明成员应看到的内容以及管理员应看到的内容。 This page that contains the following codes are in doLogin.php after the credentials in login.php are inserted in. However, I find a trouble to click back on home.php for admin as basically the home for admin is actually doLogin.php. 插入login.php中的凭据后,此页面中包含以下代码,该页面位于doLogin.php中。但是,我发现很难再单击home.php来进行管理,因为基本上,管理员的主页实际上是doLogin.php。 Often there will be a "Confirm Resubmission form" error appeared. 通常会出现“确认重新提交表格”错误。

What I wanted to do now is when the admin logs in, it should be directed to a new page which is admin.php containing all the details of what the admin should see. 我现在想做的是管理员登录时,应将其定向到一个新页面admin.php,其中包含管理员应看到的所有详细信息。 How do i do this? 我该怎么做呢?

Use header location to redirect 使用header location重定向

<?php
if (isset($_SESSION['user_id'])) {
if ($_SESSION['role'] == "Admin") {

    header("Location:admin.php");
    exit;

} elseif ($_SESSION['role'] == "Member") {

    header("Location:member.php");
    exit;
}
}
?>

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

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