【问题标题】:Session not destroying?会话没有破坏?
【发布时间】:2013-09-15 18:37:17
【问题描述】:

每当我尝试使用其他用户登录时,我看到用户名没有改变,这意味着 $_SESSION['username'] 也没有改变,那么我的 logout.php 脚本有什么问题?

<?php 
session_start();
$_SESSION = array(); 
session_unset();
session_destroy();
ob_start();
ob_end_flush();

header("location:index.php");
?>

【问题讨论】:

  • 您需要自行取消设置会话名称。
  • 试试unset($_SESSION['username']);
  • 试过 unset($_SESSION['username']);,没用
  • 你能发布你的登录脚本吗?您是否有可能在那里进行奇怪的检查以阻止您在那里设置 $_SESSION['username']?
  • @user2624407 然后你需要发布你的完整代码。如果$_SESSION['username'] = $username 尚未声明,那么如果尚未分配,您将无法取消设置它。

标签: php session


【解决方案1】:

您还需要删除会话 cookie,例如 from the reference

<?php
// Initialize the session.
// If you are using session_name("something"), don't forget it now!
session_start();

// Unset all of the session variables.
$_SESSION = array();

// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (ini_get("session.use_cookies")) {
    $params = session_get_cookie_params();
    setcookie(session_name(), '', time() - 42000,
        $params["path"], $params["domain"],
        $params["secure"], $params["httponly"]
    );
}

// Finally, destroy the session.
session_destroy();
?>

另一个很好的参考,George Brighton 在 cmets 中提到的 PHP Session Introduction

【讨论】:

  • PHP 为您做这件事——它(通常)是如何实现会话的。睡前阅读:php.net/manual/en/intro.session.php
  • 您的 php.ini 可能默认打开了 cookie ......这就是会话通常从一个调用持续到下一个调用的方式。除非您以其他方式明确执行会话以保持会话,在这种情况下,您需要以其他方式终止会话。
  • @user2624407 在您原来的问题下查看我的 cmets。
【解决方案2】:

会话是一个数组...我看不出你在做什么。

正确的不会是这样吧? ..

session_start();
$_SESSION['anything'] = array();

现在

session_destroy();

unset($_SESSION['anything']);

【讨论】:

    【解决方案3】:

    在 session_destroy() 之前尝试 unset:

    未设置($_SESSION); session_destroy();

    会话中保存的第一个销毁数据。最后一次销毁 COOKIE 会话 ID。

    【讨论】:

      猜你喜欢
      • 2011-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-13
      • 1970-01-01
      • 1970-01-01
      • 2012-11-25
      相关资源
      最近更新 更多