美文网首页
ThinkPHP5 将session保存到 mysql

ThinkPHP5 将session保存到 mysql

作者: _不能说的秘密i | 来源:发表于2019-01-28 16:39 被阅读32次

    参考

    我只是个搬运工, 一个探路者, 这个不是我写的, 原作者在这里

    为什么有这样的需求? ...鬼知道为什么不存memcache或者redis,鬼知道你会遇到什么样的需求, 那还能怎么办,自己解决呗,为了避免下次再一顿百度..一顿谷歌...我就记录一下

    建立对应的数据表

    CREATE TABLE think_session (
        session_id varchar(255) NOT NULL,
        session_expire int(11) UNSIGNED NOT NULL,
        session_data blob,
        UNIQUE KEY `session_id` (`session_id`)
    );
    

    建立数据库驱动扩展类 /extend/driver/session/Mysql.php

    默认没有这些目录和文件, 手动创建吧...

    <?php
    
    namespace driver\session;
    
    use SessionHandler;
    use think\Db;
    use think\Config;
    use think\Exception;
    
    /**
     * Class Mysql
     * session 数据库驱动
     * @package driver\session
     */
    class Mysql extends SessionHandler
    {
        protected $handler = null;
        protected $table_name = null;
        protected $config = [
            'session_expire' => 3600,           // Session有效期 单位:秒
            'session_prefix' => 'think_',       // Session前缀
            'table_name'     => 'session',      // 表名(不包含表前缀)
        ];
        protected $database = [
            'type'     => 'mysql',        // 数据库类型
            'hostname' => '127.0.0.1',    // 服务器地址
            'database' => 'my_test',       // 数据库名
            'username' => 'root',         // 用户名
            'password' => '',             // 密码
            'hostport' => '3306',         // 端口
            'prefix'   => '',             // 表前缀
            'charset'  => 'utf8',         // 数据库编码
            'debug'    => true,           // 数据库调试模式
        ];
    
        /**
         * Mysql constructor.
         * @param array $config
         * @throws Exception
         */
        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_prefix'] . $session_id,
                'session_expire' => time()
            ];
            $sql = 'SELECT session_data FROM ' . $this->table_name . ' WHERE session_id = :session_id AND session_expire > :session_expire';
            $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_prefix'] . $session_id,
                'session_expire' => $this->config['session_expire'] + time(),
                'session_data'   => $session_data
            ];
            $sql = 'REPLACE INTO ' . $this->table_name . ' (session_id, session_expire, session_data) VALUES (:session_id, :session_expire, :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_prefix'] . $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)
        {
            $sql = 'DELETE FROM ' . $this->table_name . ' WHERE session_expire < :session_expire';
            return $this->handler->execute($sql, ['session_expire' => time()]);
        }
    
    }
    

    配置

    'session'   => [
        'type'           => 'driver\session\Mysql',
        'auto_start'     => true,
        'session_expire' => 3600,
        'session_prefix' => 'think_',
        'table_name'     => 'think_session',
        'database'       => [
            'hostname' => '127.0.0.1',
            'database' => 'fa.com',
            'username' => 'root',
            'password' => '',
            'hostport' => '3306',
            'prefix'   => '',
            'charset'  => 'utf8',
        ]
    ],
    

    相关文章

      网友评论

          本文标题:ThinkPHP5 将session保存到 mysql

          本文链接:https://www.haomeiwen.com/subject/zugajqtx.html