简体   繁体   English

PHP与基于MYSQL DB角色的登录重定向

[英]PHP with MYSQL DB Role based login redirect

I've read quite a few different posts, but none seem to be helping me nail this script I'm writing for a login page. 我读了很多不同的文章,但是似乎没有人帮助我确定我为登录页面编写的脚本。

Basically I want it to do the 'normal' login check username and password against a MYSQL DB/table then based on the users assigned role forward to a specific web page. 基本上,我希望它根据MYSQL DB /表对用户分配的角色进行“常规”登录检查用户名和密码,然后将其转发给特定的网页。 The DB has four columns id, username, password, and a ROLE column. 该数据库具有四个列ID,用户名,密码和一个ROLE列。 In the ROLE column in the DB I have Superuser, Manager, Site1 or Site2 against the user names. 在数据库的ROLE列中,我拥有针对用户名的Superuser,Manager,Site1或Site2。

The script runs and at the moment dumps out on a syntax error, but I think thats my fault with not using {}'s correctly around the switch($row["ROLE"]) line. 该脚本运行并且此刻由于语法错误而转储,但是我认为这是我的错,因为在switch($ row [“ ROLE”])行周围未正确使用{}。 Previously I got the script running, but it wasn't matching the ROLE's and I was getting the echo "Wrong Login or password" message, so I know I'm close. 以前,我正在运行脚本,但该脚本与ROLE的脚本不匹配,并且收到回显“错误的登录名或密码”消息,因此我知道我已经关闭了。

Here is my checklogin PHP script so far: 到目前为止,这是我的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();
?> 

Any help or advice gladly welcomed. 任何帮助或建议都将受到欢迎。

Simon 西蒙

Update1: Ok when I modified the code and remove the $sql=SELECT... line the script runs fine no syntax issue but doesn't match the ROLE of the logged in username and displays Wrong Login or password. Update1:​​好的,当我修改代码并删除$ sql = SELECT ...行时,脚本可以正常运行,没有语法问题,但与登录用户名的角色不匹配,并显示错误的登录名或密码。

If I add back in and modify the $sql="Select.. line: 如果我重新添加并修改$ sql =“ Select ..行:

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

I get the following syntax error: 我收到以下语法错误:

Parse error: syntax error, unexpected '$sql' (T_VARIABLE), expecting ':' or '{' XXXXX on line 37 解析错误:语法错误,意外的'$ sql'(T_VARIABLE),在第37行上预期为':'或'{'XXXXX

Hmmm... 嗯...

Update2: 更新2:

Ok I think I've tidied this up a bit as per comments below: 好的,我认为我已经按照以下评论整理了一下:

$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:

Now this chucks the syntax error: 现在,这消除了语法错误:

Parse error: syntax error, unexpected '$sql' (T_VARIABLE), expecting ':' or '{' in /XXXXX on line 37 解析错误:语法错误,意外的'$ sql'(T_VARIABLE),在/ XXXXX的第37行中预期为':'或'{'

Which relates to: 涉及到:

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

Update3: 更新3:

Ok having re read the comments below I've now changed the code dumping some of the offending lines (see below). 好了,在重新阅读下面的注释之后,我现在更改了转储某些令人讨厌的行的代码(请参见下文)。

$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://

Problem I have now is I don't seem to be matching against the values in the ROLE column of the DB table and I'm not sure why. 我现在遇到的问题是我似乎与数据库表的ROLE列中的值不匹配,并且我不确定为什么。 I'm pulling all the values back with the *. 我用*拉回所有值。

As ever thoughts and observations welcomed. 一如既往的想法和意见受到欢迎。


Update 4: 更新4:

Chaps still fighting with this tried this method below using 'elseif' but not working. 仍在为此奋斗的小伙子们在下面使用“ elseif”尝试了这种方法,但是没有用。 The script runs but doesn't go beyond option 1 (Superuser) even if the ROLE is set as Manager. 该脚本可以运行,但即使ROLE设置为Manager,也不会超出选项1(超级用户)。 Any ideas? 有任何想法吗?

$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();
?>

Is my issue that I'm not matching the column of ROLE's value in the DB?? 我的问题是我不匹配数据库中ROLE值的列吗?

PS I have no syntax errors now ;) PS我现在没有语法错误;)


Update 5: Fixed it!! 更新5:修复!

My issue was using elseif instead of if and not using == in my code lines, so it should look like this... 我的问题是在代码行中使用elseif而不是if而不使用==,所以它看起来应该像这样……

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

Now I can sleep. 现在我可以睡觉了。 Thanks all for input. 谢谢大家的投入。

You have syntax errors in PHP and SQL: 您在PHP和SQL中有语法错误:

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

And then your SQL in that illegal line is wrong as well: 然后您在该非法行中的SQL也是错误的:

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

You are missing quotes around the two insert variables, which means your query will be 您缺少两个插入变量周围的引号,这意味着您的查询将是

SELECT ... WHERE username=fred and password=hunter42 SELECT ... WHERE用户名= fred和密码= hunter42

Unless you have fred and hunter42 fields in your table, that query will fail with "unknown fields" 除非您的表中有fredhunter42字段,否则该查询将失败,并显示“未知字段”


You are also mixing mysql and mysqli functions. 您还将混合使用mysql和mysqli函数。 They are NOT interchangeable, and connections/results from one are utterly useless/meaningless in the other. 它们是不可互换的,并且来自另一个的连接/结果在另一个中完全没有用/没有意义。 Plus you have variable name mismatches: 另外,您的变量名不匹配:

$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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM