TP5 Mysql数据库Session

这里给自己做一个记录TP5 版本 thinkphp_5.0.19_with_ext

1. 创建数据库表

CREATE TABLE `session` (
  `session_id` CHAR(40) NOT NULL COMMENT 'SESSION键',
  `data` VARCHAR(255) NOT NULL COMMENT 'SESSION值',
  `update_time` INT(10) UNSIGNED NOT NULL DEFAULT '0' COMMENT 'SESSION更新时间',
  PRIMARY KEY (`session_id`),
  UNIQUE KEY `session_id` (`session_id`)
) ENGINE=MEMORY DEFAULT CHARSET=utf8;

2.创建DB文件

    路径 /thinkphp/library/think/session/dirver/Db.php

<?php 

namespace think\session\driver;

use SessionHandler;

class Db extends SessionHandler
{
	protected $handler = null;
    protected $config  = [
        'table_name' => 'session', // 表名(不包含前缀)
        'expire'     => '3600',
    ];

    public function __construct($config = [])
    {
        $this->config = array_merge($this->config, $config);
    }

    /**
     * 打开Session
     * @access public
     * @param string $save_path
     * @param mixed $session_name
     * @return bool
     */
    public function open($save_path, $session_name)
    {
        if ($this->config['database'] === '') {
            $this->handler = \think\Db::name($this->config['table_name']);
        } else {
            $this->handler = \think\Db::connect($this->config['database'])->name($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)
    {
        $map['session_id'] = ['eq', $this->config['prefix'] . $session_id];
        if ($this->config['expire'] != 0) {
            $map['update_time'] = ['gt', time() - $this->config['expire']];
        }
        return $this->handler->where($map)->value('data');
    }

    /**
     * 写入Session
     * @access public
     * @param string $session_id
     * @param String $session_data
     * @return bool
     */
    public function write($session_id, $session_data)
    {
        $result = $this->handler->where('session_id', $this->config['prefix'] . $session_id)->find();
        $data   = ['session_id' => $this->config['prefix'] . $session_id, 'update_time' => time(), 'data' => $session_data];
        if ($result) {
            $affect_rows = $this->handler->update($data);
        } else {
            $affect_rows = $this->handler->insert($data);
        }

        return $affect_rows ? true : false;
    }

    /**
     * 删除Session
     * @access public
     * @param string $session_id
     * @return bool
     */
    public function destroy($session_id)
    {
        $result = $this->handler->where('session_id', $this->config['prefix'] . $session_id)->delete();
        return $result ? true : false;
    }

    /**
     * Session 垃圾回收
     * @access public
     * @param string $sessMaxLifeTime
     * @return bool
     */
    public function gc($sessMaxLifeTime)
    {
        if ($this->config['expire'] != 0) {
            $map['update_time'] = ['lt', time() - $this->config['expire']];
        } else {
            $map['update_time'] = ['lt', time() - $sessMaxLifeTime];
        }

        $result = $this->handler->where($map)->delete();
        return $result ? true : false;
    }
}

3.配置文件

    // +----------------------------------------------------------------------
    // | 会话设置
    // +----------------------------------------------------------------------

    'session'                => [
        'id'             => '',
        // SESSION_ID的提交变量,解决flash上传跨域
        'var_session_id' => '',
        // SESSION 前缀
        'prefix'         => 'think',
        // 驱动方式 支持redis memcache memcached
        'type'           => 'Db',
        // 是否自动开启 SESSION
        'auto_start'     => true,
        //如果需要Session到数据库配置数据库参数
        'database'       => [
            'type'            => 'mysql',
            // 服务器地址
            'hostname'        => '127.0.0.1',
            // 数据库名
            'database'        => 'shop',
            // 用户名
            'username'        => 'root',
            // 密码
            'password'        => 'root',
            // 端口
            'hostport'        => '3306',
        ],
    ],

4.controller 使用 session

<?php
    
     namespace app\index\controller;
     use think\Session;

     class index
     {
         public function index()
         {
             Session::set('username',array('username'=>'admin','password'=>'654321'));
             dump(Session::get('username'));
             exit;
         }

         public function delSession()
         {
             Session::delete('username');
             dump(Session::get('username'));
             exit;
         }
     }

 数据库效果

猜你喜欢

转载自my.oschina.net/u/3107609/blog/1814368