PHP SESSION 自定義會(huì)話管理器

前言

在遇到了 SESSION 混亂的問(wèn)題后,突然對(duì)自定義會(huì)話管理器有了興趣,于是便研究實(shí)現(xiàn)了一番。

分析

根據(jù)PHP官網(wǎng)的說(shuō)明,實(shí)現(xiàn)方式有兩種,這里準(zhǔn)備用繼承類(lèi)的方式做,同時(shí)將 SESSION 存儲(chǔ)于 Memcache 中。

實(shí)現(xiàn)

首先先要寫(xiě)一個(gè)繼承于 SessionHandlerInterface 的類(lèi)。

class MemcachedHanlder implements SessionHandlerInterface
{
}

MemcachedHanlder 類(lèi)實(shí)例化的時(shí)候,附上需要連接 Memcached 的參數(shù)。

protected $_options;
protected $_memcached;
protected $_prefix;

function __construct(array $options = [])
{
    if (!isset($options['host'])) {
        $options['host'] = '127.0.0.1';
    }
    if (!isset($options['port'])) {
        $options['port'] = 11211;
    }
    if (!isset($options['persistent'])) {
        $options['persistent'] = false;
    }
    if (!isset($options['lifetime'])) {
        $options['lifetime'] = null;
    }
    $this->_prefix = isset($options['prefix']) ? $options['prefix'] : '';
    $this->_options = $options;
}

繼承 SessionHandlerInterface 的類(lèi)需要實(shí)現(xiàn)幾個(gè)方法:open、close、readwrite、destorygc

open 中連接 Memcached。

/**
 * Create new session, or re-initialize existing session
 * 
 * @param  string $path
 * @param  string $name
 * @return boolean
 */
public function open($path, $name)
{
    $options = $this->_options;
    if ($options) {
        $this->_memcached = new \Memcached($options);
        $this->_memcached->addServer($options['host'], $options['port'], $options['persistent']);
        return true;
    }
    return false;
}

/**
 * Closes the current session
 * 
 * @return integer
 */
public function close()
{
    return true;
}

/**
 * Read session data
 * 
 * @param  string $id
 * @return string
 */
public function read($id)
{
    return $this->_memcached->get($this->_prefix . $id, $this->_lifetime);
}

/**
 * Read session data
 * 
 * @param  string $id
 * @param  string $data
 * @return boolean
 */
public function write($id, $data)
{
    return $this->_memcached->set($this->_prefix . $id, $data, $this->_lifetime);
}

/**
 * Destroys a session
 * 
 * @param  string $id
 * @return boolean
 */
public function destroy($id)
{
    if (!$id && $this->_memcached->exists($this->_prefix . $id)) {
        return $this->_memcached->delete($this->_prefix . $id);
    }
    return true;
}

/**
 * Cleans up expired sessions
 * 
 * @param  string $value
 * @return boolean
 */
public function gc($maxlifetime)
{
    return true;
}

Handler 類(lèi)寫(xiě)完之后,實(shí)例化它,并調(diào)用 session_set_save_handler 完成注冊(cè)。

$options = [];
$handler = new MemcachedHanlder($options);
session_set_save_handler($handler, true);

這時(shí)候,開(kāi)始暢享 SESSION 吧。

完整代碼

class MemcachedHanlder implements SessionHandlerInterface
{
    protected $_options;
    protected $_memcached;
    protected $_prefix;

    function __construct(array $options = [])
    {
        if (!isset($options['host'])) {
            $options['host'] = '127.0.0.1';
        }
        if (!isset($options['port'])) {
            $options['port'] = 11211;
        }
        if (!isset($options['persistent'])) {
            $options['persistent'] = false;
        }
        if (!isset($options['lifetime'])) {
            $options['lifetime'] = null;
        }
        $this->_prefix = isset($options['prefix']) ? $options['prefix'] : '';
        $this->_options = $options;
    }

    /**
     * Create new session, or re-initialize existing session
     * 
     * @param  string $path
     * @param  string $name
     * @return boolean
     */
    public function open($path, $name)
    {
        $options = $this->_options;
        if ($options) {
            $this->_memcached = new \Memcached($options);
            $this->_memcached->addServer($options['host'], $options['port'], $options['persistent']);
            return true;
        }
        return false;
    }

    /**
     * Closes the current session
     * 
     * @return integer
     */
    public function close()
    {
        return true;
    }

    /**
     * Read session data
     * 
     * @param  string $id
     * @return string
     */
    public function read($id)
    {
        return $this->_memcached->get($this->_prefix . $id, $this->_lifetime);
    }

    /**
     * Read session data
     * 
     * @param  string $id
     * @param  string $data
     * @return boolean
     */
    public function write($id, $data)
    {
        return $this->_memcached->set($this->_prefix . $id, $data, $this->_lifetime);
    }

    /**
     * Destroys a session
     * 
     * @param  string $id
     * @return boolean
     */
    public function destroy($id)
    {
        if (!$id && $this->_memcached->exists($this->_prefix . $id)) {
            return $this->_memcached->delete($this->_prefix . $id);
        }
        return true;
    }

    /**
     * Cleans up expired sessions
     * 
     * @param  string $value
     * @return boolean
     */
    public function gc($maxlifetime)
    {
        return true;
    }
}
$options = [];
$handler = new MemcachedHanlder($options);
session_set_save_handler($handler, true);

總結(jié)

記得之前面試的時(shí)候,有人問(wèn)過(guò)我 SESSIONCOOKIE 之間的關(guān)系,SESSION 是怎么實(shí)現(xiàn)的,SESSION 除了存文件之外,還能夠存在哪里,如何實(shí)現(xiàn)。

當(dāng)初的我都沒(méi)有回答上,想象當(dāng)初真的是太年輕了,也很天真。越接觸,越發(fā)現(xiàn)自己的渺小,越要勇往直前。

-- EOF --
本文轉(zhuǎn)載自IMJCW
原文鏈接:PHP SESSION 自定義會(huì)話管理器

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容