|
|
|
@ -17,6 +17,9 @@
|
|
|
|
|
namespace DtApp\ThinkLibrary\session;
|
|
|
|
|
|
|
|
|
|
use think\contract\SessionHandlerInterface;
|
|
|
|
|
use think\db\exception\DataNotFoundException;
|
|
|
|
|
use think\db\exception\DbException;
|
|
|
|
|
use think\db\exception\ModelNotFoundException;
|
|
|
|
|
use think\Exception;
|
|
|
|
|
use think\facade\Config;
|
|
|
|
|
use think\facade\Db;
|
|
|
|
@ -29,91 +32,48 @@ use think\facade\Db;
|
|
|
|
|
class Mysql implements SessionHandlerInterface
|
|
|
|
|
{
|
|
|
|
|
protected $handler = null;
|
|
|
|
|
protected $table_name = null;
|
|
|
|
|
protected $table_name = 'think_session'; // 表名
|
|
|
|
|
protected $config = [
|
|
|
|
|
'session_expire' => 3600, // Session有效期 单位:秒
|
|
|
|
|
'session_prefix' => 'think_', // Session前缀
|
|
|
|
|
'table_name' => 'session', // 表名(不包含表前缀)
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
protected $database = [
|
|
|
|
|
'type' => 'mysql', // 数据库类型
|
|
|
|
|
'hostname' => '127.0.0.1', // 服务器地址
|
|
|
|
|
'database' => '', // 数据库名
|
|
|
|
|
'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);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 判断数据库配置是否可用
|
|
|
|
|
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
|
|
|
|
|
* @param string $sessionId
|
|
|
|
|
* @return string
|
|
|
|
|
* @throws DbException
|
|
|
|
|
* @throws DataNotFoundException
|
|
|
|
|
* @throws ModelNotFoundException
|
|
|
|
|
*/
|
|
|
|
|
public function read(string $sessionId): string
|
|
|
|
|
{
|
|
|
|
|
$where = [
|
|
|
|
|
'session_id' => $this->config['session_prefix'] . $sessionId,
|
|
|
|
|
'session_expire' => time()
|
|
|
|
|
'session_id' => $this->config['session_prefix'] . $sessionId,
|
|
|
|
|
'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'];
|
|
|
|
|
$result = Db::table($this->table_name)
|
|
|
|
|
->where($where)
|
|
|
|
|
->find();
|
|
|
|
|
if (!empty($result)) {
|
|
|
|
|
return $result['session_data'];
|
|
|
|
|
}
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 写入Session
|
|
|
|
|
* 删除Session
|
|
|
|
|
* @param string $sessionId
|
|
|
|
|
* @return bool
|
|
|
|
|
* @throws DbException
|
|
|
|
|
*/
|
|
|
|
|
public function delete(string $sessionId): bool
|
|
|
|
|
{
|
|
|
|
|
$where = [
|
|
|
|
|
'session_id' => $this->config['session_prefix'] . $sessionId
|
|
|
|
|
];
|
|
|
|
|
$sql = 'DELETE FROM ' . $this->table_name . ' WHERE session_id = :session_id';
|
|
|
|
|
$result = $this->handler->execute($sql, $where);
|
|
|
|
|
$result = Db::table($this->table_name)
|
|
|
|
|
->where($where)->delete();
|
|
|
|
|
return $result ? true : false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -126,26 +86,12 @@ class Mysql implements SessionHandlerInterface
|
|
|
|
|
public function write(string $sessionId, string $data): bool
|
|
|
|
|
{
|
|
|
|
|
$params = [
|
|
|
|
|
'session_id' => $this->config['session_prefix'] . $sessionId,
|
|
|
|
|
'session_expire' => $this->config['session_expire'] + time(),
|
|
|
|
|
'session_data' => $data
|
|
|
|
|
'session_id' => $this->config['session_prefix'] . $sessionId,
|
|
|
|
|
'session_expire' => $this->config['session_expire'] + time(),
|
|
|
|
|
'session_data' => $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);
|
|
|
|
|
$result = Db::table($this->table_name)
|
|
|
|
|
->insert($params);
|
|
|
|
|
return $result ? true : false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Session 垃圾回收
|
|
|
|
|
* @param $sessMaxLifeTime
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function gc($sessMaxLifeTime)
|
|
|
|
|
{
|
|
|
|
|
$where = [
|
|
|
|
|
'session_expire' => time()
|
|
|
|
|
];
|
|
|
|
|
$sql = 'DELETE FROM ' . $this->table_name . ' WHERE session_expire < :session_expire';
|
|
|
|
|
return $this->handler->execute($sql, $where);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|