在登录页面,我设置会话变量与ID(我们假设1该ID始终为admin)。当用户浏览到,只有管理员有权访问的页面,check.php
充当检查是否会话变量设置一个中间人,如果是1,那么将用户重定向到管理页面,另一个页面如果事实并非如此。这里是我的代码:
<?php
include "connection.php";
session_start();
#Check to see if session variables are set properly. Only the administrator can have access to this page
if(!isset($_SESSION) || $_SESSION['id'] != 1) {
header('Location: restricted.php');
die();
} else {
header('Location: admin.php');
die();
}
?>
当我去我的登录页面,并使用管理员凭据成功登录,并尝试check.php页面,它需要我去admin.php的。如果我不登录,它仍然需要我去管理页面。
究竟是什么我此脚本问题?
因为如果条件得不到满足的人会执行。要么改变重定向在其他或创建另一组条件。
if(!isset($_SESSION) || $_SESSION['id'] != 1) { // CONDITION NOT MET
header('Location: restricted.php');
die();
} else {
header('Location: admin.php'); // REDIRECT OCCURS
die();
}