一,修改配置文件
Library/ThinkPHP/Conf/convention.php
SESSION_TYPE => "redis"
二,添加session redis驱动
Library/ThinkPHP/Think/Session/Driver/Redis.class.php
<?php
namespace ThinkSessionDriver;
class Redis {
/**
* Session有效时间
*/
protected $lifeTime = '';
/**
* 数据库句柄
*/
protected $hander;
protected $prefix = 'SESS:';
/**
* 打开Session
* @access public
*
* @param string $savePath
* @param mixed $sessName
*/
public function open($savePath, $sessName){
$this->hander = redis实例
$this->lifeTime = C('SESSION_OPTIONS.expire') ? C('SESSION_OPTIONS.expire') : ini_get('session.gc_maxlifetime');
$this->prefix = C('SESSION_PREFIX') ? C('SESSION_PREFIX') : $this->prefix;
return true;
}
/**
* 关闭Session
* @access public
*/
public function close(){
$this->gc($this->lifeTime);
$this->hander->close();
return $this->hander = null;
}
/**
* 读取Session
* @access public
*
* @param string $sessID
*/
public function read($sessID) {
$res = $this->hander->get($this->prefix.$sessID);
if(!$res){
return '';
}
return $res;
}
/**
* 写入Session
* @access public
*
* @param string $sessID
* @param String $sessData
*/
public function write($sessID, $sessData) {
$res = $this->hander->setex($this->prefix.$sessID, $sessData, $this->lifeTime);
if (!$res) {
return false;
} else {
return true;
}
}
/**
* 删除Session
* @access public
*
* @param string $sessID
*/
public function destroy($sessID) {
$count = $this->hander->delete($this->prefix.$sessID);
if ($count) {
return true;
}
return false;
}
/**
* Session 垃圾回收
* @access public
*
* @param string $sessMaxLifeTime
*/
public function gc($sessMaxLifeTime) {
// Redis 自动回收
return true;
}
}
方法二,也可以直接修改php.ini
ini_set('session.save_handler','redis');
ini_set("session.save_path","tcp://127.0.0.1:6379");