Symfony2: How to save a session attribute as a custom field on DB, with a custom PdoSessionStorage?

后端 未结 2 464
孤独总比滥情好
孤独总比滥情好 2021-01-13 16:05

I have a custom class extending PdoSessionStorage, but I don\'t know how to catch attributes from the session to save them as independent fields in the database.

Oth

相关标签:
2条回答
  • 2021-01-13 16:32

    I've found it!

    My CustomPdoSessionStorage Class extends the NativeSessionStorage, which manages the $_SESSION array (read, write, etc...).

    So, from my class I can use $this->read('userId') and store it on my DB.

    So thanks a lot @hakre

    0 讨论(0)
  • 2021-01-13 16:41

    You are using the wrong function here. unserialize is for serialized values, but not for sessions. Even those two are close to each other, they are different.

    You might be looking for the session_decode function instead. Take care it unserializes into the $_SESSION superglobal so you might want to wrap it:

    function unserialize_session($data) {
        $hasBuffer = isset($_SESSION);
        $hasBuffer && $buffer = $_SESSION;
        session_decode($data);
        $session = $_SESSION;
        $hasBuffer ? $_SESSION = $buffer : unset($_SESSION);
        return $session;
    }
    

    The counterpart is session_encode which works similarly.

    See also: How to unserialize session data in a custom handler

    0 讨论(0)
提交回复
热议问题