我一直在关注如何在php和mysql中创建登录和注销页面的在线教程,这似乎很容易直到我想要修改脚本的阶段,我试图放一个管理员用户可以访问某些链接的页面..我在我的用户表中添加了一个名为user_level的列,我希望当用户登录时他的user_level为1,他将访问season.php脚本中的链接但是如果他的user_level是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 " <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");
}
?>
答案 0 :(得分:0)
您的语法错误,如果您想要显示链接,则不应重定向,它应该类似于:
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;
}
答案 1 :(得分:0)
此外,使用header("Location: client.php");
时,请确保这是您脚本中出现的第一个内容。你不能转出整个html页面然后把它放在底部,它不会重定向。