2013-05-07 69 views
2

因此,我正在制作一个简单的PHP项目,我没有太多的PHP使用经验。如果用户访问级别2或更高,则重定向

也许你们可以帮助我。

我有一个数据库至少存储用户crendentials,看起来像这样。

id|Username|Password|   Mail  |Acces| 
1 |Josh | ***** |[email protected]| 1 | 
2 |Rick | **0** |[email protected]| 2 | 

而且我的PHP页面上我想使它所以如果艾策斯平出的数据库是高于1它重定向。我正在用echo的权利知道,所以我一直没有重定向。但每次我这样做,我只是得到我没有足够的权利。而我使用用户名Josh登录。

$sql = "SELECT * From login Where Username= "'$_SESSION('username')'""; 
while($row = mysql_fetch_row($sql)); 
if ($row['Acces'] == "1"){ 
Echo("You have enough rights"); 
}else{ 
Echo("You dont have enough rights"); 
}; 

任何帮助表示赞赏

+4

[**在新的代码,请不要使用'mysql_ *'功能**](HTTP:/ /bit.ly/phpmsql)。他们不再被维护[并被正式弃用](https://wiki.php.net/rfc/mysql_deprecation)。看到[**红框**](http://j.mp/Te9zIL)?学习[*准备的语句*](http://j.mp/T9hLWi),并使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [这篇文章](http://j.mp/QEx8IB)将帮助你决定哪个。如果你选择PDO,[这里是一个很好的教程](http://j.mp/PoWehJ)。 – h2ooooooo 2013-05-07 07:28:53

+0

您目前正在检查访问级别是否等于“1”。您应该将其修改为>(高于)。 – 2013-05-07 07:35:26

回答

1

你猜对了部分正确的,但我会作出一些改变:

$sql = mysql_query("SELECT * FROM login WHERE Username= '".mysql_real_escape_string($_SESSION['username'])."'"); 
$sql_fetched = mysql_fetch_assoc($sql); 

if ($sql_fetched['Acces'] > 1) 
{ 
    echo "You have enough rights and will be redirected automatically."; 
    header('refresh:3;url=redirect.php'); 
} 
else 
{ 
    echo "You dont have enough rights."; 
    header('refresh:3;url=login.php'); 
} 
+0

仍然有不健康的SQL注入量,除非$ _SESSION是某种功能,否则这将不起作用。 – h2ooooooo 2013-05-07 07:28:25

+0

是的它的工作,检查两个帐户,并知道重定向和消息出现! 非常感谢。 – Innominatum 2013-05-07 07:34:46

0
$sql = "SELECT * From login Where Username= "'$_SESSION('username')'""; 
while($row = mysql_fetch_row($sql)); 
if ($row['Acces'] == "1"){ 
header('Location: http://www.example.com/'); /* Redirect browser */ 
}else{ 
echo('You dont have enough rights'); 
}; 

更改http://www.example.com/到你想要的一个。

+0

带有'Location'的头文件需要一个绝对URI,而不仅仅是'somewhere.php' – Wilq 2013-05-07 07:26:48

+0

首先,你使用'''连接字符串? '.'是PHP中正确的运算符。其次,你使用'('和')'访问'$ _SESSION'数组*,即使*,在PHP中访问数组的方式是通过括号'['和']'。我知道这只是从OP粘贴,但你应该纠正它。 – h2ooooooo 2013-05-07 07:27:00

+0

哦,太困了。对不起。 'header('Location:http://www.example.com/');/*重定向浏览器* /' – NMALKO 2013-05-07 07:29:03

0
$sql = "SELECT * FROM `login` Where `Username`='{$_SESSION['username']}'"; 

while($row = mysql_fetch_row($sql)) { 
    if ($row['Acces'] == "1") { 
     // Change these two headers to the file you want to redirect too 
     header("Location: http://www.yoursite.com/enoughrights.php"); 
    } else { 
     header("Location: http://www.yoursite.com/notenough.php"); 
    } 
}