PHP与MYSQL数据库基于角色的登录重定向


PHP with MYSQL DB Role based login redirect

我读过很多不同的帖子,但似乎没有一篇能帮助我确定我为登录页面编写的脚本。

基本上,我希望它根据MYSQL数据库/表进行"正常"登录检查用户名和密码,然后根据用户分配的角色转发到特定的网页。DB有四列id、用户名、密码和一列ROLE。在DB的ROLE列中,我有Superuser、Manager、Site1或Site2作为用户名。

脚本运行时出现语法错误,但我认为这是我的错,因为我没有在开关($row["ROLE"])行正确使用{}。之前我运行了脚本,但它与角色不匹配,并且我收到了"登录或密码错误"的回复消息,所以我知道我已经接近了。

到目前为止,这是我的checklogin PHP脚本:

<?php
ob_start();
$host="XXXXXX"; // Host name 
$username="XXXXXX"; // Mysql username 
$password="XXXXXX"; // Mysql password 
$db_name="XXXXXX"; // Database name 
$tbl_name="XXXXXX"; // Table name 
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
// Define $myusername and $mypassword 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 
// To protect MySQL injection
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name 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){
$row = mysqli_fetch_array($rslt, MYSQLI_ASSOC);
switch($row["ROLE"])
$sql="SELECT ROLE FROM $tbl_name WHERE username={$myusername} and password={$mypassword}";
{
    case 'Superuser':
    header("location:http://www.XXXXXX.com/1/index.html");   
    break;
case 'Manager':
    header("location:http://www.XXXXXX.com/2/index.html");   
break;
case 'Site1':
    header("location:http://www.XXXXXX.com/3/index.html");   
break;
case 'Site2':
    header("location:http://www.XXXXXX.com/4/index.html");   
break;
default:
echo "Wrong Login or password";
}
}
else {
header("location:login_fail.php");
}
ob_end_flush();
?> 

欢迎任何帮助或建议。

Simon

Update1:好的,当我修改代码并删除$sql=SELECT。。。行,脚本运行良好,没有语法问题,但与登录用户名的角色不匹配,并显示错误登录名或密码。

如果我添加回并修改$sql="Select..line:

switch($row['ROLE']) 
$sql="SELECT ROLE FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; 

我得到以下语法错误:

分析错误:语法错误,意外的"$sql"(T_VARIABLE),第37行应为":"或"{"XXXXX

嗯。。。

更新2:

好的,我想我已经按照下面的评论整理了一下:

$sql="SELECT * FROM $tbl_name WHERE myusername='$myusername' and mypassword='$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){
$row = mysql_fetch_array($rslt, MYSQL_ASSOC);

switch($row['ROLE'])
$sql="SELECT ROLE FROM $tbl_name WHERE myusername='$myusername' and mypassword='$mypassword'";
{

     case 'Superuser':
     header("location:

现在这抛出语法错误:

分析错误:语法错误,意外的"$sql"(T_VARIABLE),第37行/XXXXXXX中应为":"或"{"

与以下内容有关:

 $sql="SELECT ROLE FROM $tbl_name WHERE myusername='$myusername' and mypassword='$mypassword'";

更新3:

好的,在重读了下面的评论之后,我现在已经更改了代码,丢弃了一些有问题的行(见下文)。

$sql="SELECT * FROM $tbl_name WHERE myusername='$myusername' and mypassword='$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){
// ??
$row = mysql_fetch_array($rslt, MYSQL_ASSOC);

switch( $row['ROLE']){
case 'Superuser':
header("location:http://

我现在遇到的问题是,我似乎没有与DB表的ROLE列中的值匹配,我不知道为什么。我用*把所有的价值都拉回来了。

一如既往地欢迎各种想法和意见。


更新4:

仍在与之斗争的Chaps在下面使用"elseif"尝试了这种方法,但没有奏效。脚本运行,但不会超出选项1(超级用户),即使ROLE设置为Manager。有什么想法吗?

$sql = "SELECT * FROM $tbl_name 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" 
$_SESSION['username'] = $myusername; 
$_SESSION['password'] = $mypassword; 
$result = mysql_fetch_array($result); // get the result set from the query
$redirect = trim($result['ROLE']); // get the redirect column's value
if ($redirect == '') 
{echo "No redirect value set";} 
elseif ($redirect="Superuser"){header("Location: http://www.xxxx.com/1/index.html");}
elseif ($redirect="Manager"){header("Location: http://www.xxxx.com/2/index.html");}
elseif ($redirect="User1"){header("Location: http://www.xxxx.com/3/index.html");}
elseif ($redirect="User2"){header("Location: http://www.xxxx.com/4/index.html");}
    exit; 
}
else 
{ echo "Wrong Username or Password"; } 
ob_end_flush();
?>

我的问题是我没有匹配数据库中ROLE值的列吗??

PS我现在没有语法错误;)


更新5:修复了它!!

我的问题是在代码行中使用elseif而不是if,并且不使用==,所以它应该是这样的。。。

 if ($redirect=="Superuser"){header("Location: http://www.xxxxx.com/1/index.html");}

现在我可以睡觉了。感谢大家的投入。

PHP和SQL中存在语法错误:

switch($row['ROLE']) {
   $sql = ".."; // illegal. a switch can contain only `case` and `default` clauses.

然后你在那个非法行中的SQL也是错误的:

$sql="SELECT ROLE FROM $tbl_name WHERE username={$myusername} and password={$mypassword}";
                                                ^-----------^              ^-----------^

您在两个插入变量周围缺少引号,这意味着您的查询将是

选择。。。其中用户名=fred,密码=hunter42

除非表中有fredhunter42字段,否则该查询将以"未知字段"失败


您还混合了mysql和mysqli函数。它们是不可互换的,其中一个的连接/结果在另一个中是完全无用/毫无意义的。另外,变量名称不匹配:

$result=mysql_query($sql);
^^^^^^^--- note this variable
             ^---note the lack of an "i"
$row = mysqli_fetch_array($rslt, MYSQLI_ASSOC);
            ^----note the "i"
                           ^^^^--note the different variable