From dd7068e3aee509a19d4dc99eb34546884fa089b3 Mon Sep 17 00:00:00 2001 From: ELF <360197197@qq.com> Date: Thu, 19 Nov 2020 10:48:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E7=94=A8redis=E5=AD=98=E5=82=A8sessio?= =?UTF-8?q?n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Application/Admin/Model/MemberModel.class.php | 4 - Application/Common/Conf/config.php | 5 + ThinkPHP/Common/functions.php | 2 +- .../Think/Session/Driver/Redis.class.php | 120 ++++++++++++++++++ 4 files changed, 126 insertions(+), 5 deletions(-) create mode 100644 ThinkPHP/Library/Think/Session/Driver/Redis.class.php diff --git a/Application/Admin/Model/MemberModel.class.php b/Application/Admin/Model/MemberModel.class.php index 35bef2aa3..cc4181628 100644 --- a/Application/Admin/Model/MemberModel.class.php +++ b/Application/Admin/Model/MemberModel.class.php @@ -85,10 +85,6 @@ class MemberModel extends Model { 'show_market_admin'=>$adminData['show_market_admin'], 'show_promote'=>$adminData['show_promote'], ); - //登录有效时间改为1小时 - $expireTime = 3600; - ini_set('session.gc_maxlifetime', $expireTime); - ini_set('session.cookie_lifetime', 0); session('user_group_id',$groupId); session('user_auth', $auth); session('user_auth_sign', data_auth_sign($auth)); diff --git a/Application/Common/Conf/config.php b/Application/Common/Conf/config.php index 90383c743..069b8da5d 100644 --- a/Application/Common/Conf/config.php +++ b/Application/Common/Conf/config.php @@ -91,7 +91,12 @@ $config = array( // session 配置数组 支持type name id path expire domain 等参数 'SESSION_OPTIONS' => array( // 'expire' => 3600 + 'type' => 'Redis', + 'prefix' => 'sess_', + 'path' => 'tcp://127.0.0.1:6379', + 'expire' => 60, ), + 'SESSION_REDIS_EXPIRE' => 3600, ); $config = array_merge($config, $env); return array_merge($config, $cache_config); diff --git a/ThinkPHP/Common/functions.php b/ThinkPHP/Common/functions.php index c0ec3d771..799da5b63 100644 --- a/ThinkPHP/Common/functions.php +++ b/ThinkPHP/Common/functions.php @@ -1245,7 +1245,7 @@ function session($name='',$value='') { if(isset($name['domain'])) ini_set('session.cookie_domain', $name['domain']); if(isset($name['expire'])) { ini_set('session.gc_maxlifetime', $name['expire']); - ini_set('session.cookie_lifetime', $name['expire']); + ini_set('session.cookie_lifetime', 99999999); } if(isset($name['use_trans_sid'])) ini_set('session.use_trans_sid', $name['use_trans_sid']?1:0); if(isset($name['use_cookies'])) ini_set('session.use_cookies', $name['use_cookies']?1:0); diff --git a/ThinkPHP/Library/Think/Session/Driver/Redis.class.php b/ThinkPHP/Library/Think/Session/Driver/Redis.class.php new file mode 100644 index 000000000..e50e21a0f --- /dev/null +++ b/ThinkPHP/Library/Think/Session/Driver/Redis.class.php @@ -0,0 +1,120 @@ + '127.0.0.1', + 'port' => 6379, + 'password' => null, + 'select' => 1, + 'expire' => 3600, + 'timeout' => 0, + 'persistent' => true, + 'session_name' => 'session_', + ]; + + public function __construct($config = []) + { + $this->config['host'] = C("SESSION_REDIS_HOST") ? C("SESSION_REDIS_HOST") : $this->config['host']; + $this->config['port'] = C("SESSION_REDIS_POST") ? C("SESSION_REDIS_POST") : $this->config['port']; + $this->config['password'] = C("SESSION_REDIS_AUTH") ? C("SESSION_REDIS_AUTH") : $this->config['password']; + $this->config['select'] = C("SESSION_REDIS_SELECT") ? C("SESSION_REDIS_SELECT") : $this->config['select']; + $this->config['expire'] = C("SESSION_REDIS_EXPIRE") ? C("SESSION_REDIS_EXPIRE") : $this->config['expire']; + $this->config['session_name'] = C('SESSION_PREFIX') ? C('SESSION_PREFIX') : $this->config['session_name']; + $this->config['timeout'] = C('SESSION_CACHE_TIMEOUT') ? C('SESSION_CACHE_TIMEOUT') : $this->config['timeout']; + } + + /** + * 打开Session + * @access public + * @param string $savePath + * @param mixed $sessName + * @return bool + * @throws Exception + */ + public function open($savePath, $sessName) + { + // 检测php环境 + if (!extension_loaded('redis')) { + throw new Exception('not support:redis'); + } + $this->handler = new \Redis; + // 建立连接 + $func = $this->config['persistent'] ? 'pconnect' : 'connect'; + $this->handler->$func($this->config['host'], $this->config['port'], $this->config['timeout']); + if ('' != $this->config['password']) { + $this->handler->auth($this->config['password']); + } + if (0 != $this->config['select']) { + $this->handler->select($this->config['select']); + } + return true; + } + + /** + * 关闭Session + * @access public + */ + public function close() + { + $this->gc(ini_get('session.gc_maxlifetime')); + $this->handler->close(); + $this->handler = null; + return true; + } + + /** + * 读取Session + * @access public + * @param string $sessID + * @return string + */ + public function read($sessID) + { + return (string)$this->handler->get($this->config['session_name'] . $sessID); + } + + /** + * 写入Session + * @access public + * @param string $sessID + * @param String $sessData + * @return bool + */ + public function write($sessID, $sessData) + { + if ($this->config['expire'] > 0) { + return $this->handler->setex($this->config['session_name'] . $sessID, $this->config['expire'], $sessData); + } else { + return $this->handler->set($this->config['session_name'] . $sessID, $sessData); + } + } + + /** + * 删除Session + * @access public + * @param string $sessID + * @return bool + */ + public function destroy($sessID) + { + return $this->handler->delete($this->config['session_name'] . $sessID) > 0; + } + + /** + * Session 垃圾回收 + * @access public + * @param string $sessMaxLifeTime + * @return bool + */ + public function gc($sessMaxLifeTime) + { + return true; + } +} \ No newline at end of file