创建管理员';s页面,这样用户就无法访问其他页面


creating an admin's page so that the user can not access other pages

我一直在学习如何在php和mysql中创建登录和注销页面的在线教程,这似乎很容易,直到我准备好修改脚本,我才尝试将管理员页面放在用户可以访问一些链接的地方。。我在users表中添加了一个名为userlevel的列,我希望当用户登录时,如果他的userlevel为1,他将访问season.php脚本中的链接,但如果他的userLevel为2,他将被引导到另一个页面。。。我试着在下面这样做,但不起作用

if($user_level == 1){
header("Location: links.php");
}
else($user_level == 2){
header("Location: client.php");
}

这是我的session.php代码

<?php
if(!isset($_SESSION['name'])&&isset($_COOKIE['testsite'])){
    $_SESSION['name'] = $_COOKIE['testsite'];
}
$dir = "profiles/".$_SESSION['name']."/images/";
$open = opendir($dir);
while(($file = readdir($open)) != FALSE){
    if($file!="."&&$file!=".."&&$file!="Thumbs.db"){
        echo "<img border='1' width='70' height='70' src='$dir/$file'>";
    }
}
echo "&nbsp<b>".$_SESSION['name']."</b>'s session<br /><a href='logout.php'>Logout</a>";
if($user_level == 1){
header("Location: links.php");
}
else($user_level == 2){
header("Location: client.php");
}
?>

您的语法是错误的,如果您想显示链接,则不应该重定向,它可能应该类似于:

if($user_level == 1){
  // don't redirect here
  include "links.php";
}
// check the php manual for the correct usage of if / else / elseif
elseif ($user_level == 2){
  header("Location: client.php");
  // terminate the script
  exit;
}

此外,在使用header("Location: client.php");时,请确保这是脚本中的第一个内容。你不能把整个html页面都转储掉,然后把它放在底部,它不会重定向。