改用redis存储session

master
ELF 4 years ago
parent 6a50654045
commit dd7068e3ae

@ -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));

@ -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);

@ -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);

@ -0,0 +1,120 @@
<?php
namespace Think\Session\Driver;
use think\Exception;
class Redis implements \SessionHandlerInterface
{
/** @var \Redis */
protected $handler = null;
protected $config = [
'host' => '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;
}
}
Loading…
Cancel
Save