PHP会话异常,重新启动浏览器后会话变量仍然存在

时间:2018-08-13 07:47:55

标签: php

Logout.php脚本:

session_start();
session_destroy();
session_start();

unset($_SESSION['admin_uname']);
session_regenerate_id();

$_SESSION['success_msg'] = "<strong>You've been logged out.</strong>";
header('location: //domain.com/admin/login');
exit;

Login.php(部分):

if (isset($_SESSION['admin_uname']) && !empty($_SESSION['admin_uname'])) {
  goPage("//domain.com/admin/dashboard"); // goPage is a selfmade PHP function that checks whether value is self, home or an url and redirects the user to the correct location
  exit;
}

Core.php //核心位于每一页的所有内容之上。下面的脚本检查用户是否在受保护的页面上,这些页面在$protectedpages数组中定义。

if (isset($_SESSION['admin_uname']) && !empty($_SESSION['admin_uname'])) {
  $admin_uname = $_SESSION['admin_uname'];
} else {
  $protectedpages = array("contact", "offertes");
  $currentpage = str_replace(".php", "", basename($_SERVER['PHP_SELF']));
  if (in_array($currentpage, $protectedpages)) {
    $_SESSION['error_msg'] = 'Your session either expired or you are not logged in. Please try again.';
    header('location: //domain.com/admin/login');
    exit;
  }
}

当用户通过进入logout.php页面注销时。关闭浏览器,重新打开浏览器,返回到login.php页面上的if (isset($_SESSION['admin_uname'])代码的login.php部分,用户将经过core.php由于消息login.php未检测到要登录的用户或者至少未检测到Your session either expired or you are not logged in. Please try again.被设置,因此返回消息core.php返回到$_SESSION['admin_uname']页面或不为空。通常,您会期望这种行为触发无限循环,但它不会那样做。

我希望一切都有意义,我将其范围缩小到上面的代码。该脚本没有其他部分可以设置$_SESSION['admin_uname']变量。

0 个答案:

没有答案