• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php 如何以json格式存储session,而不是默认的内置编码?

php 搞代码 4年前 (2022-01-22) 31次浏览 已收录 0个评论
文章目录[隐藏]

php 如何以json格式存储session,而不是默认的内置编码?

折腾了下,即使session_save_handler被自己的类或者方法重写,write与read的出入数据都还是被序列化的,而且被session序列化不是一般的序列化…还是不能解解决memcached保存session数据为json的格式

回复内容:

php 如何以json格式存储session,而不是默认的内置编码?

折腾了下,即使session_save_handler被自己的类或者方法重写,write与read的出入数据都还是被序列化的,而且被session序列化不是一般的序列化…还是不能解解决memcached保存session数据为json的格式

找到了方案:

<code><?phpnamespace Lboy\Session\SaveHandler;/** * Memcached JSON-formatted session save handler * * The default memcache session save handler stores sessions encoded with * session_encode, but the encoded session is not simple to parse in other * languages. Therefore, this class encodes the session in JSON to make reading * the session in other languages simple. * * Note: This class uses the newer php-memcached extension, not php-memcache! * @see http://php.net/manual/en/book.memcached.php * * @author Lee Boynto<a style="color:transparent">、本文来源gao($daima.com搞@代@#码$网</a><big>搞gaodaima代码</big>n  */class Memcached{    /**     * @var \Memcached     */    protected $memcached;    /**     * Create new memcached session save handler     * @param \Memcached $memcached     */    public function __construct(\Memcached $memcached)    {        $this->memcached = $memcached;    }    /**     * Close session     *     * @return boolean     */    public function close()    {        return true;    }    /**     * Destroy session     *     * @param string $id     * @return boolean     */    public function destroy($id)    {        return $this->memcached->delete("sessions/{$id}");    }    /**     * Garbage collect. Memcache handles this with expiration times.     *     * @param int $maxlifetime     * @return boolean Always true     */    public function gc($maxlifetime)    {        // let memcached handle this with expiration time        return true;    }    /**     * Open session     *     * @param string $savePath     * @param string $name     * @return boolean     */    public function open($savePath, $name)    {        // Note: session save path is not used        $this->sessionName = $name;        $this->lifetime = ini_get('session.gc_maxlifetime');        return true;    }    /**     * Read session data     *     * @param string $id     * @return string     */    public function read($id)    {        $_SESSION = json_decode($this->memcached->get("sessions/{$id}"), true);        if (isset($_SESSION) && !empty($_SESSION) && $_SESSION != null)        {            return session_encode();        }        return '';    }    /**     * Write session data     *     * @param string $id     * @param string $data     * @return boolean     */    public function write($id, $data)    {        // note: $data is not used as it has already been serialised by PHP,        // so we use $_SESSION which is an unserialised version of $data.        return $this->memcached->set("sessions/{$id}", json_encode($_SESSION),            $this->lifetime);    }}</code>

可以考虑下写库


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php 如何以json格式存储session,而不是默认的内置编码?
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址