3.0变动是比较大的,session支持了redis,memcache,files,database。文件的就不说了,这个是最简单的。这里说一下使用数据库、redis来存储session的方法。
首选,因为其手册没有更新,手册讲的是2.0版本的使用方法。3.0已经不是这样使用。这里使用数据库的方式,最大的变化是数据库变了。
| 代码如下 | 复制代码 |
| CREATE TABLE IF NOT EXISTS `ci_sessions` ( `id` varchar(40) NOT NULL, `ip_address` varchar(45) NOT NULL, `timestamp` int(10) unsigned DEFAULT 0 NOT NULL, `data` blob NOT NULL, PRIMARY KEY (id), KEY `ci_sessions_timestamp` (`timestamp`) ); | |
请使用这个建数据库,应该根据其配置说明应该就会使用数据库的存储方式了。
当然如果你重构了他的数据库操作类,尤其是增加了自动主从读写分离等,那么你需要也重构这个session_database.php了,这个有问题可以交流,也很简单。ci的好处就可以很方便重构。
2、使用redis
这个我是看他代码才知道怎么用的。
看下这个文件。Session_redis_driver.php
| 代码如下 | 复制代码 |
public function __construct(&$params) { parent::__construct($params); if (empty($this->_config['save_path'])) { log_message('error', 'Session: No Redis save path configured.'); } elseif (preg_match('#(?:tcp://)?([^:?]+)(?::(d+))?(?.+)?#', $this->_config['save_path'], $matches)) { isset($matches[3]) OR $matches[3] = ''; // Just to avoid undefined index notices below $this->_config['save_path'] = array( 'host' => $matches[1], 'port' => empty($matches[2]) ? NULL : $matches[2], 'password' => preg_match('#auth=([^s&]+)#', $matches[3], $match) ? $match[1] : NULL, 'database' => preg_match('#database=(d+)#', $matches[3], $match) ? (int) $match[1] : NULL, 'timeout' => preg_match('#timeout=(d+.d+)#', $matches[3], $match) ? (float) $match[1] : NULL ); preg_match('#prefix=([^s&]+)#', $matches[3], $match) && $this->_key_prefix = $match[1]; } else { log_message('error', 'Session: Invalid Redis save path format: '.$this->_config['save_path']); } if ($this->_config['match_ip'] === TRUE) { $this->_key_prefix .= $_SERVER['REMOTE_ADDR'].':'; } } | |
根据这个构造函数,我们发现,redis的连接信息全在save_path这个配置项。所以我们可以这样配置
| 代码如下 | 复制代码 |
| $config['sess_save_path'] = '127.0.0.1:6379?auth=admin&prefix=ci_sess:&database=2&timeout=60'; | |
这个地方,你根据代码就可以看出来,ip,端口。然后是后面的password就是匹配的auth=之后,后面就很简单了。prefix是session存储的前缀,database是库号,redis是分库的。timeout连接超时时间
模拟极限越野
模拟极限越野开着大巴在各种奇葩路况里送乘客。山路弯道多得让人
模拟火车
模拟火车让你当回真正的火车司机,得先学会看信号灯、操作控制杆
可口的披萨美味的披萨
可口的披萨美味的披萨带您体验经营一家披萨店的乐趣。游戏画风温
定制虚拟偶像最新版
定制虚拟偶像最新版是一款超级经典有趣的日本模拟装扮类型的手游
病娇模拟器手机版正版(yandere simulator)
病娇模拟器最新版手游是一款萌妹二次元模拟游戏,玩家在游戏中可