我有一个登录数据库登录重定向基于角色的PHP
username | password | role
xxxxxx xxxxxxx Admin
xxxxxx xxxxxxx Trainer
xxxxxx xxxxxxx Client
xxxxxx xxxxxxx Line Manager
是有可能在登录拥有管理员角色重定向到admin.php的人与培训师的角色登录到trainer.php有人用客户端角色登录到client.php。
我有一个登录数据库登录重定向基于角色的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");
}
喜欢这个
如何获得有效用户的角色? – user2612747
@user它是*你的*数据库!如何在不知道如何获取他的信息的情况下登录您的用户? – deceze
@ user2612747在SQL查询中获取给定用户名的角色并编译它 – Backtrack
假设您已经检索到的用户信息从数据库中,并保存他们的角色到一个会话变量,它像这样简单......
<?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;
Anthony。
简答:是的。
您可以检查从数据库用户的角色,然后你可以用switch或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 VAR可以让你检查谁访问管理员用户。 php,client.php或trainer.php确实是一个经过验证的管理员/客户/培训师。
我会用这样的(但可能使用一个数据库表来存储重定向的作用,使其更具动态):
$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!");
您正在将JS和PHP语法混合在一起。 ;)我试过编辑,但显然1个字符不足以进行编辑。 –
'$ redir'赋值上的'@'?是的,忘了'$','@'是故意的。 :) – Mario
不,连接。 :) –
假设您的登录表称为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":
//.....
}
是有可能 –
是的,这是完全可能的。 – deceze
这是可能的。 –