自定义 php SessionHandler 的自定义 serialize_handler(数据库存储) [英] Custom serialize_handler for custom php SessionHandler (DB storage)

查看:75
本文介绍了自定义 php SessionHandler 的自定义 serialize_handler(数据库存储)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 php 中使用 3rd 方 (django) 会话管理的过程中,我需要有自定义序列化函数,以便正确编码/解码它以适应 django 对会话数据的盐渍存储.目前,似乎 ini 设置 session.serialize_handler 可以是 php 或 wddx.

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.

有没有办法将自定义的 serialize_handler 设置为一个类?

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

我想要这样的东西:

class CustomSessionSerializer {

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

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

并让我的自定义 SessionHandler 使用它.

and have it used by my custom SessionHandler.

igbinary github 上的项目 似乎添加了自定义 serialize_handler 作为 php扩大.我很好奇自定义序列化是否不会发生在其他地方而不是作为 C 扩展.

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.

推荐答案

我一直在处理这个问题,并且有一个解决方案.

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

这个想法是,虽然您可以轻松地从 PHP 修改 session.serializer_handler,但您可以在运行序列化程序之前清空 $_SESSION 的内容.
使用一个类来管理会话(如 Zend\Session\SessionManager),其中使用 register_shutdown_function 注册一个函数,在该函数中传递回 save_handler$_SESSION 内容的副本,然后 $_SESSION 为空.

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.

这样序列化程序会在空字符串上运行,并且自定义序列化在您的自定义 save_handler 上执行.

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

这篇关于自定义 php SessionHandler 的自定义 serialize_handler(数据库存储)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆