-7

我有一个登录数据库

username | password | role

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

登录时是否有可能具有管理员角色的人重定向到 admin.php 具有培训师角色的人登录到 trainer.php 具有客户端角色的人登录到 client.php。

4

6 回答 6

5

Assuming you have already retrieved the user info from the database, and stored their role into a Session variable, its as simple as this...

<?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");}
于 2013-07-29T07:12:20.890 回答
1

首先获取有效用户的角色并像这样比较

 $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");
 }

像这样

于 2013-07-29T07:12:06.203 回答
1

简短的回答:是的。

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

 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; }

$_SESSION变量允许您检查访问 admin.php、client.php 或 trainer.php 的用户是否真的是经过身份验证的管理员/客户端/培训师。

于 2013-07-29T07:15:02.493 回答
0

我会使用这样的东西(但可能使用数据库表来存储角色重定向以使其更加动态):

$redirect = array(
    'Admin' => 'http://example.com/admin.php',
    'Trainer' => 'http://example.com/trainer.php',
    'Client' => 'http://example.com/client.php',
    //...
    );

if ($redir = @$redirect[$role]) {
    header('Location: ' . $redir);
    ob_end_clean(); // In case you've had output buffered already; optional.
    exit();
}
else
    die("Unknown role: $role; don't know where to go!");
于 2013-07-29T07:16:02.710 回答
0

当然,只需存储角色 -> 家庭关系并查找它,例如

<?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;

安东尼。

于 2013-07-29T07:14:37.900 回答
0

假设您的登录表被调用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":
    //.....
}
于 2013-07-29T07:18:12.367 回答