首页 > 代码库 > thinkphp框架下session 存入mongo

thinkphp框架下session 存入mongo


最近做系统的时候处理到session入数据库问题 , 由于是在thinkphp框架下, 查看了下框架session相关代码, 发现原框架默认支持mysql数据库 , 

于是对原Session驱动做了相应修改 , 使其支持mongo数据库存储session

修改的相关文件是\Think\Session\Driver\Db\Db.class.php , 本人使用的是TP3.2,2版本 , 修改后的Db.class.php代码如下:


<?php
namespace Think\Session\Driver;

class Db
{
    const MONGO_EXPIRY = 3;
    private $host = '';
    private $port = '';
    private $username = '';
    private $password = '';
    private $dbname = '';
    private $dbsession = '';
    private $__mongo_collection = NULL;
    private $__current_session = NULL;

    /**
     * Default constructor set default parameter
     * @access public
     */
    public function __construct()
    {
        $this->host = C('DB_HOST');
        $this->port = C('DB_PORT');
        $this->username = C('DB_USER');
        $this->password = C('DB_PWD');
        $this->dbname = C('DB_NAME');
        $this->dbsession = C('SESSION_TABLE');
        $this->__connect();
        session_set_save_handler(
            array(&$this, 'open'),
            array(&$this, 'close'),
            array(&$this, 'read'),
            array(&$this, 'write'),
            array(&$this, 'destroy'),
            array(&$this, 'gc')
        );
    }

    /**
     * connectes the mongo database and create collection
     * @access private
     */
    private function __connect()
    {
        $connection_string = sprintf('mongodb://%s:%s', $this->host, $this->port);
        if ($this->username != null && $this->password != null) {
            $connection_string = "mongodb://{$this->username}:{$this->password}@{$this->host}:{$this->port}";
            $connection_string = sprintf('mongodb://%s:%s@%s:%s', $this->username, $this->password, $this->username, $this->password);
        }
        $object_mongo = new \Mongo($connection_string);
        $object_mongo = $object_mongo->{$this->dbname};
        $this->__mongo_collection = $object_mongo->{$this->dbsession};
    }


    /**
     *
     * check for collection object
     * @access public
     * @param string $session_path
     * @param string $session_name
     * @return boolean
     */
    public function open($session_path, $session_name)
    {
        $result = false;
        if ($this->__mongo_collection != NULL) {
            $result = false;
        }
        return $result;
    }

    /**
     *
     * doing noting
     * @access public
     * @return boolean
     */
    public function close()
    {
        return true;
    }

    /**
     *
     * Reading session data based on id
     * @access public
     * @param string $session_id
     * @return mixed
     */
    public function read($session_id)
    {
        $result = NULL;
        $expiry = time();
        $query['_id'] = $session_id;
        $query['expiry'] = array('$gte' => $expiry);
        $result = $this->__mongo_collection->findone($query);
        if ($result) {
            $this->__current_session = $result;
        }
        return $result['data'];
    }

    /**
     *
     * Writing session data
     * @access public
     * @param string $session_id
     * @param mixed $data
     * @return boolean
     */
    public function write($session_id, $data)
    {
        $result = true;
        $expiry = $this->__getExpriry();
        $session_data = http://www.mamicode.com/array();>



thinkphp框架下session 存入mongo