Recently when the system was processed to the session into the database problem, because it is under the framework of thinkphp, the following framework session related code, found that the original framework supports MySQL database by default,
Therefore, the original session driver was modified to support the MONGO database storage session
Modify the relevant file is \think\session\driver\db\db.class.php, I am using the tp3.2,2 version, the modified Db.class.php code is as follows:
<?phpnamespace 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 (&A MP, $this, ' read '), Array (& $this, ' write '), Array (& $this, ' Destroy '), Array (&$ This, ' GC '); }/** * ConnEctes the MONGO database and create collection * @access private */Private Function __connect () {$c onnection_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:%[email protected]%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 strin G $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 m ixed */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 = Array (); if (Empty ($this->__current_session)) {$session _id = $session _id; $session _data[' _id ') = $session _id; $session _data[' data ' = $data; $session _data[' expiry ') = $expiry; } else {$session _data = (array) $this->__current_session; $session _data[' data ' = $data; $session _data[' expiry ') = $expiry; } $query [' _id '] = $session _id; $record = $this->__mongo_collection->findone ($query); if ($record = = null) {$this->__mongo_collection->insert ($session _data); } else {$record [' data '] = $data; $record [' expiry '] = $expiry; $this->__mongo_collection->save ($record); } return true; }/** * Remove session data * @access public * @param string $session _id * @return Boolean */ Public Function DestroY ($session _id) {$query [' _id '] = $session _id; $this->__mongo_collection->remove ($query, true); return true; }/** * * Garbage Collection * @access public * @return Boolean */Public Function GC () { $query = Array (); $query [' expiry '] = Array (': lt ' = time ()); $this->__mongo_collection->remove ($query, Array (' justone ' = false)); return true; }/** * Get expiry * @access private * @return int */Private Function __getexpriry () {RE Turn time () + Self::mongo_expiry; }}
Thinkphp in the framework of the session deposit MONGO