如果用户具有访问级别2或更高级别,则重定向

时间:2013-05-07 07:22:48

标签: php mysql

所以我正在制作一个简单的PHP项目,而且我对PHP没有多少经验。

也许你们可以帮助我。

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

id|Username|Password|          Mail       |Acces|
1 |Josh    | *****  |Something@hotmail.com|  1  |
2 |Rick    | **0**  |Generalpo@hotmail.com|  2  |

在我的php页面上,我想这样做,如果数据库中的Acces级别高于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");
};

感谢任何帮助

3 个答案:

答案 0 :(得分: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');
}

答案 1 :(得分: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/更改为您想要的。

答案 2 :(得分: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");
    }
}