Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom serialize_handler for custom php SessionHandler (DB storage)

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.

like image 266
aminho Avatar asked Mar 30 '12 18:03

aminho


1 Answers

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.

like image 66
Rainier Avatar answered Sep 30 '22 12:09

Rainier