PHP:会话数据显示但被破坏

问题描述 投票:0回答:1

我有一个

class SessionHandling
,它有以下基本方法:

class SessionHandling
{

    protected function startSession()
    {
        if (session_status() == PHP_SESSION_NONE) {
            session_start();
    }

    protected function destroySession()
    {
        $this->startSession();
        session_destroy();
        $_SESSION = [];
    }

    protected function getSessionData($key)
    {
        $this->startSession();
        return $_SESSION[$key] ?? null;
    }

    protected function setSessionData($key, $value)
    {
        $this->startSession();
        $_SESSION[$key] = $value;
    }

    protected function handleSessionStatus()
    {
        $sessionStatus = session_status();
        if ($sessionStatus === PHP_SESSION_NONE) {
        echo "Session has been destroyed.";
    } else {
        echo "Session is still active.";
    }
}

然后我简单地测试一下这些方法是否有效:

public function show()
{
    $this->setSessionData("user", ["id" => 1, "name" => "John Doe"]);

    $userData = $this->getSessionData("user");

    $this->destroySession();

    $this->handleSessionStatus();

    $this->dd($userData);

}

我得到的输出是

Session has been destroyed.
,我仍然得到数组:

Array
(
    [id] => 1
    [name] => John Doe
)

会话被破坏了,但我仍然在输出中得到数组,这是怎么回事?我错过了什么吗?

php arrays oop session methods
1个回答
0
投票

尝试以下代码:

unset($_SESSION);

并测试一下:

@session_unset();
© www.soinside.com 2019 - 2024. All rights reserved.