【问题标题】:PHP logout failed, unset($_SESSION['user']) does not workPHP 注销失败,unset($_SESSION['user']) 不起作用
【发布时间】:2019-02-26 07:23:54
【问题描述】:

这是一个多星期以来的问题,我开始相信它与代码以外的东西有关!

这是我的会话页面:

<?php
session_start();
include('dbConfigBDO.php');
include('SafeRedirect.php');

if(!isset($_SESSION['login_user'])){
    safe_redirect('login.php');
    die();
}
?>

我在登录后设置$_SESSION['login_user'] 并在每个页面中调用session.php。我还有一个注销页面:

<?php
session_start();
require 'SafeRedirect.php';
unset($_SESSION['login_user']);
session_destroy();
safe_redirect('login.php');
?>

即使在注销后我仍然可以访问我试图通过使用会话来阻止的页面。

可能有什么问题?

编辑:我的SafeRedirect.php 页面,我从here 使用它

<?php
function safe_redirect($url, $exit=true) {
    if (!headers_sent()){

        header('HTTP/1.1 301 Moved Permanently');
        header('Location: ' . $url);

        header("Connection: close");
    }

    print '<html>';
    print '<head><title>Redirecting you...</title>';
    print '<meta http-equiv="Refresh" content="0;url='.$url.'" />';
    print '</head>';
    print '<body onload="location.replace(\''.$url.'\')">';

    print 'You should be redirected to this URL:<br />';
    print "<a href=$url>'$url'</a><br /><br />";

    print 'If you are not, please click on the link above.<br />';

    print '</body>';
    print '</html>';

    if ($exit) exit;
}
?>

【问题讨论】:

  • 你能发布 SafeRedirect.php 的内容吗?
  • 我做了,感谢您的编辑!
  • 老实说,我没有看到问题..
  • 对我来说也一样,真正的问题是它曾经在另一个项目中工作:/

标签: php html session


【解决方案1】:

确保在

之后需要session.php
if(!isset($_SESSION['login_user'])){
          safe_redirect('login.php');
          die();
       }

【讨论】:

  • 你在哪里初始化 $_SESSION['login_user']?
  • 您是否可以在询问变量是否为 isset 并调用 safe_redirect() 之前对其进行初始化?
【解决方案2】:

php.ini 不需要这个问题。这可能是浏览器缓存一些数据的问题。你有没有在你的 html 中尝试过这些头像?

<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />

【讨论】:

  • 没有改变,我将它添加到我的索引和登录页面。
  • index.php 还是看到用户登录了?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-01-03
  • 2011-09-12
  • 1970-01-01
  • 1970-01-01
  • 2018-06-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多