【发布时间】:2013-07-26 19:04:17
【问题描述】:
我需要根据数据库中赋予用户的角色将用户重定向到不同的页面。登录页面只提交用户名和密码。我必须从如下所示的数据库中获取角色:
username | password | role
xxxxxx xxxxxx admin
xxxxxx xxxxxx trainer
xxxxxx xxxxxx trainer
xxxxxx xxxxxx Line Manager
这是我的代码:
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$sql="SELECT * FROM login WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
$role = mysqli_fetch_array($result);
if($role == "Admin"){
header("location:index.php");
exit();
}
elseif($role['role'] == "Trainer"){
header("location:index1.php");
exit();
}
elseif($role['role'] == "Line Manager"){
header("location:index2.php");
exit();
}
elseif($role['role'] == "Client"){
header("location:client.php");
exit();
}
}
else {
echo "Wrong Username or Password";
}
?>
【问题讨论】:
-
好的,告诉我们更多。这似乎工作正常,我会使用 switch 块而不是 if
-
阅读ACL's 以更好地了解您需要做什么。此外,由于没有实际问题,因此不清楚您在这里问我们什么。
-
标头位置应为 URI。不是文件名。将该文件与基本网址一起使用。
-
它不起作用,页面仍然存在,没有任何重定向,你能告诉我 switch 块如何与我的代码一起工作
-
第一个
if检查$role,而其他检查$role['role']。你可能想要后者