How does php store sessions in json format instead of the default built-in encoding?

Source: Internet
Author: User
How does php store sessions in json format instead of the default built-in encoding? After all, even if session_save_handler is overwritten by its own classes or methods, the inbound and outbound data of write and read is serialized, and session serialization is not a general serialization... still not... how does php store sessions in json format instead of the default built-in encoding?

After all, even if session_save_handler is overwritten by its own classes or methods, the inbound and outbound data of write and read is serialized, and session serialization is not a general serialization... still cannot solve the problem that memcached saves session data in json format

Reply content:

How does php store sessions in json format instead of the default built-in encoding?

After all, even if session_save_handler is overwritten by its own classes or methods, the inbound and outbound data of write and read is serialized, and session serialization is not a general serialization... still cannot solve the problem that memcached saves session data in json format

Found the solution:


   */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);    }}

You can consider writing a database.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.