php中Session默認都是用文件存儲的,thinkphp是提供了redis和Memcache的存儲類,但是沒有提供MySQL的儲存類,現在修改為用數據庫的方式儲存,直接上代碼:
<?php /** * session 存mysql類 * dcr163.cn */ namespace thinksessiondriver; use SessionHandler; use thinkDb; use thinkConfig; use thinkException; /** * 數據庫方式Session驅動 * 表結構 * CREATE TABLE `tp_session` ( * `session_id` VARCHAR(150) NOT NULL COMMENT 'session_key', * `session_data` TEXT COMMENT 'session_值', * `expire_time` int(11) NOT NULL DEFAULT '0' COMMENT '過期時間', * KEY `idx_session_id` (`session_id`), * UNIQUE KEY `session_id` (`session_id`), * KEY `idx_expire_time` (`expire_time`) * ) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COMMENT='session表'; * */ class Mysql extends SessionHandler { protected $handler = null; protected $table_name = null; protected $config = [ 'expire_time' => 3600, // Session有效期 單位:秒 'session_name' => 'tp_', // Session前綴 'table_name' => 'session', // 表名(不包含表前綴) ]; protected $database = [ 'type' => 'mysql', // 數據庫類型 'hostname' => '127.0.0.1', // 服務器地址 'database' => '', // 數據庫名 'username' => 'root', // 用戶名 'password' => '', // 密碼 'hostport' => '3306', // 端口 'prefix' => 'tp_', // 表前綴 'charset' => 'utf8', // 數據庫編碼 'debug' => true, // 數據庫調試模式 ]; public function __construct($config = []) { // 獲取數據庫配置 if(isset($config['database']) && !empty($config['database'])) { if(is_array($config['database'])){ $database = $config['database']; }elseif(is_string($config['database'])){ $database = Config::get($config['database']); }else{ throw new Exception('session error:database'); } unset($config['database']); }else{ // 使用默認的數據庫配置 $database = Config::get('database'); } $this->config = array_merge($this->config, $config); $this->database = array_merge($this->database, $database); } /** * 打開Session * @access public * @param string $save_path * @param mixed $session_name * @return bool * @throws Exception */ public function open($save_path, $session_name) { // 判斷數據庫配置是否可用 if(empty($this->database)){ throw new Exception('session error:database empty'); } $this->handler = Db::connect($this->database); $this->table_name = $this->database['prefix'] . $this->config['table_name']; return true; } /** * 關閉Session * @access public */ public function close() { $this->gc(ini_get('session.gc_maxlifetime')); $this->handler = null; return true; } /** * 讀取Session * @access public * @param string $session_id * @return bool|string */ public function read($session_id) { $where = [ 'session_id' => $this->config['session_name'] . $session_id, 'expire_time' => time() ]; $sql = 'SELECT session_data FROM ' . $this->table_name . ' WHERE session_id = :session_id AND expire_time > :expire_time'; $result = $this->handler->query($sql, $where); if(!empty($result)){ return $result[0]['session_data']; } return ''; } /** * 寫入Session * @access public * @param string $session_id * @param String $session_data * @return bool */ public function write($session_id, $session_data) { $params = [ 'session_id' => $this->config['session_name'] . $session_id, 'expire_time' => $this->config['expire_time'] + time(), 'session_data' => $session_data ]; $sql = 'REPLACE INTO ' . $this->table_name . ' (session_id, expire_time, session_data) VALUES (:session_id, :expire_time, :session_data)'; $result = $this->handler->execute($sql, $params); return $result ? true : false; } /** * 刪除Session * @access public * @param string $session_id * @return bool|void */ public function destroy($session_id) { $where = [ 'session_id' => $this->config['session_name'] . $session_id ]; $sql = 'DELETE FROM ' . $this->table_name . ' WHERE session_id = :session_id'; $result = $this->handler->execute($sql, $where); return $result ? true : false; } /** * Session 垃圾回收 * @access public * @param string $sessMaxLifeTime * @return bool */ public function gc($sessMaxLifeTime) { $where = [ 'expire_time' => time() ]; $sql = 'DELETE FROM ' . $this->table_name . ' WHERE expire_time < :expire_time'; return $this->handler->execute($sql, $where); } }
把上面的文件保存為:Mysql.php
放在目錄:/thinkphp/library/think/session/driver 下;
然后在配置文件中修改session的儲存方式,文件位置 : /Application/config.php
'session' => [ 'id' => '', // SESSION_ID的提交變量,解決flash上傳跨域 'var_session_id' => '', // SESSION 前綴 'prefix' => 'think', // 驅動方式 支持redis memcache memcached 'type' => 'mysql', // 是否自動開啟 SESSION 'auto_start' => true, // mysql 存session的表名 'table_name' => 'session', // 是否自動開啟 SESSION ],
以上就是TP5把session存儲到Mysql中的步驟,這時打開頁面就可以看到session已經存到mysql表里了