php 如何以json格式儲存session,而不是預設的內建編碼?

WBOY
發布: 2023-03-01 17:12:02
原創
1361 人瀏覽過

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><?php

namespace 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 Boynton <lee@lboynton.com>
 */
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>
登入後複製

可以考慮下寫庫

相關標籤:
php
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!