2013-07-29 42 views
-6

我有一个登录数据库登录重定向基于角色的PHP

username | password | role 

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

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

+6

是有可能 –

+2

是的,这是完全可能的。 – deceze

+2

这是可能的。 –

回答

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

喜欢这个

+0

如何获得有效用户的角色? – user2612747

+1

@user它是*你的*数据库!如何在不知道如何获取他的信息的情况下登录您的用户? – deceze

+0

@ user2612747在SQL查询中获取给定用户名的角色并编译它 – Backtrack

5

假设您已经检索到的用户信息从数据库中,并保存他们的角色到一个会话变量,它像这样简单......

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

Anthony。

1

简答:是的。

您可以检查从数据库用户的角色,然后你可以用switchif结构重定向:

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 VAR可以让你检查谁访问管理员用户。 php,client.php或trainer.php确实是一个经过验证的管理员/客户/培训师。

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!"); 
+0

您正在将JS和PHP语法混合在一起。 ;)我试过编辑,但显然1个字符不足以进行编辑。 –

+0

'$ redir'赋值上的'@'?是的,忘了'$','@'是故意的。 :) – Mario

+0

不,连接。 :) –

0

假设您的登录表称为users,您已经连接到您的DB和用户填写某种登录表单后加载这个页面:

$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": 
    //..... 
} 
+0

你应该在HTTP响应的头部使用'Location'。如果独立的话,考虑到它是格式错误的HTML,将随机独立的'meta'标签吐出并不是你应该做的事情,浏览器甚至可能忽略它。它也会导致你的浏览器首先显示页面;甚至可能避免基于安全/用户设置的重定向。 – Mario

+0

@Mario我改变了它。我使用它,因为我们无法知道提问者将把这个PHP代码放在哪里,并希望避免任何“Headers already sent”错误。 – lazyCrab

+1

那么,那是一个不同的故事。 :)你也可以假设可能已经有了一个上一行,其中也已经设置了“位置”。 – Mario