Custom serialize_handler for custom php SessionHandler (DB storage)

为君一笑 提交于 2019-12-22 05:23:08

问题


In the process of using 3rd party (django) session management within php, i need to have custom serialization functions in order to encode/decode it properly to fit django's salted storage of session data. Currently, it appears that the ini setting session.serialize_handler can either be php or wddx.

Is there any way to set up a custom serialize_handler as a class?

I'd like to have something like this:

class CustomSessionSerializer {

    public static function serialize($data){
    // Serializes raw data
    }

    public static function unserialize($sdata){
    // Deserializes serialized data
    }
}

and have it used by my custom SessionHandler.

igbinary project on github seems to add a custom serialize_handler as a php extension. I'm curious if custom serialization could not happen in another place than as a C extension.


回答1:


I've been dealing with this problem, and there is a solution for this.

The idea is that although you can easily modify the session.serializer_handler from PHP, you can empty the contents of $ _SESSION before running the serializer.
Using a class for the administration of session (like Zend\Session\SessionManager) in which is registered with register_shutdown_function a function, in which is passed back to save_handler a copy of $ _SESSION content and then $ _SESSION is empty.

So that the serializer runs but on empty string, and the custom serialization is executed on your custom save_handler.




回答2:


You can use session_set_save_handler() to use your own session handling functions

In PHP 5.4 you can use SessionHandlerInterface.

By default you will receive already serialized data, so you will have to unserialize it and use your own serialization routines.




回答3:


Might look like a work-around but it does what you need. The serialization applies when your custom session handler receives the $_SESSION superglobal and you need to return it from the read handler as serialized. But, you can store the session as any serialization or format or anything you want.

Example

class SessionHandler {

    public function __construct() {
       session_set_save_handler(
            array($this, 'open')
           ,array($this, 'close')
           ,array($this, 'read')
           ,array($this, 'write')
           ,array($this, 'destroy')
           ,array($this, 'gc')
        );
    }

    public function open($savePath, $sessionName) {
        return true;
    }

    public function close() {
        return true;
    }

    public function read($id) {
        $data = CustomStorage::fetchSessionData($id);
        return serialize(
            CustomSerialization::unserialize($data);
        );
    }

    public function write($id, $serializedData) {
        CustomStorage::writeSessionData(
             $id
            ,CustomSerialization::serialize(unserialize($serializedData))
        );
        return true;
    }

    //gc and destroy
}

Although not pretty and with a bit of overhead, but you only need to control the serialization when storing, so it should do the trick.

Hope it helps!



来源:https://stackoverflow.com/questions/9948182/custom-serialize-handler-for-custom-php-sessionhandler-db-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!