【问题标题】:print still writes old Session variable value after destroy销毁后 print 仍然写入旧的 Session 变量值
【发布时间】:2013-01-12 07:41:43
【问题描述】:

我使用了一些 Session 变量。当我注销时,函数 show_number 应该写 0 数字但它没有。

注销.php:

<?php
session_start();
session_destroy();
require_once('upper.php');
show_number(); //this function is declared on upper.php
?>

index.php:

<?php
$_SESSION['var'] = 1;
echo "<a href="logout.php">Logout</a>
?>

upper.php:

function show_number() { // shows value of $_SESSION['var'];
if (isset($_SESSION['var']))
  echo "1";
else
  echo "0";
}

问题是: 当我单击注销链接时,回显仍然写入数字 1,我必须重新加载页面才能看到 0 值。

干杯

【问题讨论】:

    标签: php session destroy


    【解决方案1】:

    session_destroy不会清除全局$_SESSION对象:

    它不会取消设置与 会话,或取消设置会话 cookie。

    清除代码中的会话数据:

    session_start();
    session_destroy();
    $_SESSION = array();
    

    或者更彻底地消除所有会话数据(来自同一文档页面):

    <?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();
    ?>
    

    【讨论】:

      猜你喜欢
      • 2017-07-27
      • 2021-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-25
      • 1970-01-01
      • 1970-01-01
      • 2013-11-13
      相关资源
      最近更新 更多