简体   繁体   English

破坏旧的会话,使新的会话,但php仍指旧的会话

[英]destroying old session, making new, but php still refers to old session

Took me a while to tackle this problem to it's exact cause, but here's what seems to be happening: 花了我一段时间来解决这个问题的确切原因,但这似乎正在发生:

I have a session. 我开会 I want to completely kill my current session and start from scratch, with a brand new session, that has a blank slate. 我想完全取消我的当前会话并从头开始,使用一个全新的会话,该会话具有空白状态。

So this is what I do: 所以这就是我要做的:

public function unregister_session()
{
    // I COMMENTED THOSE SECTIONS THAT I WASNT SURE WHAT THEY WERE DOING, BUT PROBLEM PERSISTS.
    //session_regenerate_id();
    //$params = session_get_cookie_params();
        // setcookie(session_name(), '', time() - 42000,
            // $params["path"], $params["domain"],
            // $params["secure"], $params["httponly"]);
    unset($_SESSION);
    $_SESSION=array();
    echo '<br> destroying session. old SID:'.session_id(); //echos 'qqhu7on0n...'
    session_unset();
    session_destroy();
    echo '<br> limbo SID:'.session_id(); //echos nothing.
    session_start();
    echo '<br> new SID:'.session_id();  //echos 'qqhu7on0n...'
}   

Alright so what i think should happen is that I have a new session. 好吧,所以我认为应该进行的是我要召开新的会议。 And well it kind of works, because everything about the previous session seems to be forgotten, at least if I look at $_SESSION . 这样做很好,因为至少在我看$_SESSION ,上一届会议的所有内容似乎都被遗忘了。

BUT whenever I echo the session_id it still gives me the old session ID! 但是,每当我回显session_id时,它仍然给我旧的会话ID! When I write any values into $_SESSION they are not carried over to the next page, instead on the next page $_SESSION is empty! 当我将任何值写入$ _SESSION时,它们不会被带到下一页,而是在下一页$_SESSION为空!

EDIT: i echo the session_id() on multiple places on my script (going from top to bottom) i get always the same session_id displayed. 编辑:我在我脚本的多个位置(从上到下)回显session_id(),我总是显示相同的session_id。 going into google developer tools looking at my cookies, i see a different id for PHPSESSID. 进入Google开发人员工具查看我的Cookie,我看到了一个不同的PHPSESSID ID。 i see the exact id which i will see when i'm trying to echo session_id() on the next page... 我在下一页上回显session_id()时会看到确切的ID ...

Why is this happening and what am I doing wrong? 为什么会这样,我在做什么错? How can I get session_id() to show me the NEW session id, not the old one? 如何获取session_id()向我显示新的会话ID,而不是旧的ID? How can I write values into the NEW $_SESSION variable, so that they are actually carried over to the next page? 如何将值写到新的$ _SESSION变量中,以便将它们实际带到下一页?

EDIT - THE SOLUTION 编辑-解决方案

public function unregister_session()
{
    // DUNNO IF THE COMMENTED SECTIONS MAKE A DIFFERENCE
    //$params = session_get_cookie_params();
        // setcookie(session_name(), '', time() - 42000,
            // $params["path"], $params["domain"],
            // $params["secure"], $params["httponly"]);
    unset($_SESSION);
    $_SESSION=array();
    echo '<br> destroying session. old SID:'.session_id(); //echos 'qqhu7on0n...'
    session_unset();
    session_destroy();
    echo '<br> limbo SID:'.session_id(); //echos nothing.
    session_start();
    session_regenerate_id(TRUE); //THIS DOES THE TRICK! Calling it after session_start. Dunno if true makes a difference.
    echo '<br> new SID:'.session_id();  //echos '7b2jn...' :-)
}   

Make sure you are calling session_start on whatever page is calling that function. 确保在正在调用该函数的任何页面上都调用session_start。 I would also un-comment the code for destroying the cookie. 我还将取消注释销毁cookie的代码。 That can possibly prevent weird problems with cached data. 这样可以避免缓存数据出现怪异问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM