Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于角色php的登录重定向_Php - Fatal编程技术网

基于角色php的登录重定向

基于角色php的登录重定向,php,Php,我有一个登录数据库 username | password | role xxxxxx xxxxxxx Admin xxxxxx xxxxxxx Trainer xxxxxx xxxxxxx Client xxxxxx xxxxxxx Line Manager 登录时,具有管理员角色的人是否可能重定向到Admin.php具有培训师角色的人登录到Trainer.php具有客户机角色的人登录到Client.php。首先获取有效用户的角色,并

我有一个登录数据库

username | password | role

xxxxxx     xxxxxxx    Admin
xxxxxx     xxxxxxx    Trainer
xxxxxx     xxxxxxx    Client
xxxxxx     xxxxxxx    Line Manager

登录时,具有管理员角色的人是否可能重定向到Admin.php具有培训师角色的人登录到Trainer.php具有客户机角色的人登录到Client.php。

首先获取有效用户的角色,并进行如下比较

 $Role  = query_database("SELECT * FROM YOUR_TABLE_NAME WHERE username='".$_POST["username"]."'");


if ( $Role == "Admin")
 {
    header("location:www.example.com/admin.php");
 }
  else  if ( $Role == "Trainer ")
 {
          header("location: www.example.com/Trainer.php");
 }

像这样

假设您已经从数据库中检索到用户信息,并将其角色存储到会话变量中,就这么简单

<?php
 session_start();
 $role = $_SESSION['role'];

 if($role=="Admin"){header("location: www.yourpage.com/admin.php");}
 elseif($role=="Trainer"){header("location: www.yourpage.com/trainer.php");}
 elseif($role=="Client"){header("location: www.yourpage.com/client.php");}

当然,只要存储角色->家庭关系并查找它,就像

<?php
function get_home_by_role($role)
{
    $home = 'admin.php';

    $roles = array(
        'client'    => 'client.php',
        'trainer'   => 'trainer.php',
    );

    if (isset($roles[$role])) {
        $home = $roles[$role];
    }

    return $home;
}
$user = get_user();
$home = get_home_by_role($user->role);
header("Location: http://example.org/$home");
exit;
简短回答:是

您可以从数据库中为用户检查角色,然后可以使用或结构重定向:

 switch ($role) {
     case 'admin':
         $_SESSION['role'] = 'admin';
         // redirect to admin
         header( 'Location: admin.php');
         break;
     case 'client':
         $_SESSION['role'] = 'client';
          // redirect to client
         header( 'Location: client.php');
         break;
      case 'trainer':
         $_SESSION['role'] = 'trainer';
          // redirect to admin
         header( 'Location: trainer.php');
         break; }

var允许您检查访问admin.php、client.php或trainer.php的用户是否真的是经过身份验证的管理员/client/trainer。

我会使用类似的方法(但可能会使用数据库表来存储角色重定向,使其更具动态性):


假设您的登录表名为
users
,则您已连接到数据库,用户在填写某种登录表单后正在加载此页面:

$q = "SELECT * FROM users WHERE username='".$_POST["username"]."'";

$result = mysql_query($q) or die(mysql_error());

$row = mysql_fetch_array($result);

switch($row["role"])
{
    case "Admin":
       header("Location: http://".$_SERVER['SERVER_NAME']."/admin.php");
       die();
       break;
    case "Trainer":
    //.....
}

是的,这是可能的眼睛,这是完全可能的。你能告诉我怎么做吗?你能告诉我们你到目前为止尝试了什么吗?我如何为有效用户获取角色?@user这是你的数据库!如何在不知道如何获取用户信息的情况下登录用户?@user2612747在SQL查询中获取给定用户名的角色,实际上,我认为您的第一行代码是误导性的,因为您只分配了一个字符串,特别是考虑到您以后也在使用字符串。而是将这样的东西包装在一个未指定但会说话的函数(名称)中;类似于
$role=query\u数据库(“从您的\u表\u名称中选择*)
您应该在HTTP响应的头中使用
Location
。你不应该随意地抛出一个独立的
meta
标记,浏览器甚至可能会忽略它,因为如果是独立的,它的HTML格式是错误的。它也会导致浏览器首先显示页面;甚至可能避免基于安全/用户设置的重定向。@Mario我更改了它。我之所以使用它,是因为我们不知道询问者将把这些PHP代码放在哪里,并且希望避免任何“Headers ready sent”错误您还可以假设前面的一行中也设置了
位置
,您将JS和PHP语法混为一谈我试过编辑,但显然1个字符还不够编辑。
$redir
作业上的
@
?是的,忘记了
$
@
是故意的。:)哎呀。。。也解决了这个问题。
$q = "SELECT * FROM users WHERE username='".$_POST["username"]."'";

$result = mysql_query($q) or die(mysql_error());

$row = mysql_fetch_array($result);

switch($row["role"])
{
    case "Admin":
       header("Location: http://".$_SERVER['SERVER_NAME']."/admin.php");
       die();
       break;
    case "Trainer":
    //.....
}