doumengyin0491 2016-03-24 09:35
浏览 58
已采纳

三种不同的角色,三种不同的观点

I'm currently in a school-project and we are doing a site where there are three different types of roles.

In our user-table we have an attribute named "role". This role has a table with 3 different values, 1, 2 and 3.

1 (Admin) 2 (Adminuser) 3 (user).

What I want is: When someone is logging in, the code wants to look for what value in role this user has and then redirect this user to "admin.php" (if the value is 1), "adminuser.php" (if the value is 2) or "user.php" if the value is 3. This is the current code, and it works so that you can login, but you get sent to the same page regardless.

if(isset($_POST['email']))
{
 $query = <<<END
 SELECT email, password, userID, FROM user
 WHERE email = '{$_POST['email']}'
AND password = '{$_POST['password']}'
END;
$res = $mysqli->query($query);
if ($res->num_rows > 0)
{
 $row = $res->fetch_object();
 $_SESSION["email"] = $row->email;
 $_SESSION["userID"] = $row->id;
header("Location:admin.php");
  }
  else
 {
 echo "Fel email eller lösenord.";
 }
}
  • 写回答

1条回答 默认 最新

  • donglie1898 2016-03-24 09:55
    关注

    If the role is in the user table then try this with a simple array. Below the simple code from where you can get your desire output.

    $role = array("1"=> "Admin.php", "2"=> "Adminuser.php", "3"=> "user.php");
    if(isset($_POST['email'])){
      $query = <<<END
      SELECT email, password, userID, id FROM user
      WHERE email = '{$_POST['email']}'
      AND password = '{$_POST['password']}'
      END;
      $res = $mysqli->query($query);
      if ($res->num_rows > 0){
        $row = $res->fetch_object();
        $_SESSION["email"] = $row->email;
        $_SESSION["userID"] = $row->id;
    
        //retrieve the user role from user-table, i use userid, you may use your own field
        $query2 = <<<END
        SELECT role FROM user-table
        WHERE userid = '{$row->id}'
        END;
        $res2 = $mysqli->query($query2);
        $row2 = $res2->fetch_object();
    
        header("Location: ".$role[$row2->role]);
      }else{
        echo "Fel email eller lösenord.";
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)