2013-05-07 68 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"); 
    } 
}