因为某些原因。 IF 语句有效....我试过 If/else 也没有运气
For some reason. IF statement is working....Ive tried If/else with no luck either
只有 'Admin' if 语句运行正确。其他任何内容都会跳转到未嵌套的其他内容...有什么原因吗?
注意:是的,我检查了排名的拼写...它们拼写正确
将在用户成功登录后重定向
(登录-sucessful.php):
<?php
session_start();
if(isset($_SESSION["username"])){
if ($_SESSION["rank"] == 'Admin'){//Check if Admin
header("location:/panel/admin/profile.php");
} elseif ($_SESSION["rank"] == 'Faculty'){//Check if Faculty
header("location:/panel/student/profile.php");
} elseif ($_SESSION["rank"] == 'Student'){//Check if Student
header("location:/panel/faculty/profile.php");
}
}else{
header("location:log-in.php");
}
?>
登录页面(login.php):
<?php
session_start();
$host = "localhost";
$username = "username";
$password = "password!";
$database = "database";
$message = "";
try
{
$connect = new PDO("mysql:host=$host; dbname=$database", $username, $password);
$connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if(isset($_POST["login"]))
{
if(empty($_POST["username"]) || empty($_POST["password"]))
{
$message = '<label class="red-text text-darken-2">All fields are required</label>';
}
else
{
$query = "SELECT * FROM login WHERE username = :username AND password = :password";
$statement = $connect->prepare($query);
$statement->execute(
array(
'username' => $_POST["username"],
'password' => $_POST["password"]
)
);
$count = $statement->rowCount();
if($count > 0)
{
$_SESSION["username"] = $_POST["username"];
$_SESSION["rank"] = $statement->fetchColumn(3);
header("location:login_success.php");
}
else
{
$message = '<label class="red-text text-darken-2">Wrong Email or Password</label>';
}
}
}
}
catch(PDOException $error)
{
$message = $error->getMessage();
}
?>
登录Table:
Login Table
不知道确切原因(为此需要更多代码)但请尝试这样做
if (empty($_SESSION['username']) && empty($_SESSION['rank'])){
header("location:log-in.php");
}
else if ($_SESSION["rank"] == 'Admin'){//Check if Admin
header("location:/panel/admin/profile.php");
} else if ($_SESSION["rank"] == 'Faculty'){//Check if Faculty
header("location:/panel/faculty/profile.php");
} else if ($_SESSION["rank"] == 'Student'){//Check if Student
header("location:/panel/student/profile.php");
}
哇....真不敢相信我只花了一个小时....现在是凌晨 2 点,我已经筋疲力尽了...一定是这个问题出现的原因....} elseif ($_SESSION["rank "] == 'Faculty'){//检查Faculty header("location:/panel/student/profile.php"); } elseif ($_SESSION["rank"] == 'Student'){//检查Student header("location:/panel/faculty/profile.php");链接到错误目录的文件迫使它返回登录页面(因为不允许排名)非常感谢你们......我觉得自己像个笨蛋......现在要睡觉
只有 'Admin' if 语句运行正确。其他任何内容都会跳转到未嵌套的其他内容...有什么原因吗?
注意:是的,我检查了排名的拼写...它们拼写正确
将在用户成功登录后重定向 (登录-sucessful.php):
<?php
session_start();
if(isset($_SESSION["username"])){
if ($_SESSION["rank"] == 'Admin'){//Check if Admin
header("location:/panel/admin/profile.php");
} elseif ($_SESSION["rank"] == 'Faculty'){//Check if Faculty
header("location:/panel/student/profile.php");
} elseif ($_SESSION["rank"] == 'Student'){//Check if Student
header("location:/panel/faculty/profile.php");
}
}else{
header("location:log-in.php");
}
?>
登录页面(login.php):
<?php
session_start();
$host = "localhost";
$username = "username";
$password = "password!";
$database = "database";
$message = "";
try
{
$connect = new PDO("mysql:host=$host; dbname=$database", $username, $password);
$connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if(isset($_POST["login"]))
{
if(empty($_POST["username"]) || empty($_POST["password"]))
{
$message = '<label class="red-text text-darken-2">All fields are required</label>';
}
else
{
$query = "SELECT * FROM login WHERE username = :username AND password = :password";
$statement = $connect->prepare($query);
$statement->execute(
array(
'username' => $_POST["username"],
'password' => $_POST["password"]
)
);
$count = $statement->rowCount();
if($count > 0)
{
$_SESSION["username"] = $_POST["username"];
$_SESSION["rank"] = $statement->fetchColumn(3);
header("location:login_success.php");
}
else
{
$message = '<label class="red-text text-darken-2">Wrong Email or Password</label>';
}
}
}
}
catch(PDOException $error)
{
$message = $error->getMessage();
}
?>
登录Table: Login Table
不知道确切原因(为此需要更多代码)但请尝试这样做
if (empty($_SESSION['username']) && empty($_SESSION['rank'])){
header("location:log-in.php");
}
else if ($_SESSION["rank"] == 'Admin'){//Check if Admin
header("location:/panel/admin/profile.php");
} else if ($_SESSION["rank"] == 'Faculty'){//Check if Faculty
header("location:/panel/faculty/profile.php");
} else if ($_SESSION["rank"] == 'Student'){//Check if Student
header("location:/panel/student/profile.php");
}
哇....真不敢相信我只花了一个小时....现在是凌晨 2 点,我已经筋疲力尽了...一定是这个问题出现的原因....} elseif ($_SESSION["rank "] == 'Faculty'){//检查Faculty header("location:/panel/student/profile.php"); } elseif ($_SESSION["rank"] == 'Student'){//检查Student header("location:/panel/faculty/profile.php");链接到错误目录的文件迫使它返回登录页面(因为不允许排名)非常感谢你们......我觉得自己像个笨蛋......现在要睡觉