1. 存到memcached中, 十分简单, 在使用session之前, 加入下面两行代码
int_set('session.save_handler', 'memcache');
int_set('session.save_path', '127.0.0.1:11211');
2.存到数据库里面, 重新定义了session的生命周期
<?php /** * Class SessionDBTool */ class SessionDBTool{ private $link; //数据库连接对象 public function __construct(){ //自定义session处理方法 ini_set('session.save_handler','user'); session_set_save_handler( array($this,'sess_open'), array($this,'sess_close'), array($this,'sess_read'), array($this,'sess_write'), array($this,'sess_destroy'), array($this,'sess_gc') ); //开启session @session_start(); } //开启session public function sess_open(){ $this->link = MYSQLDB::getInstance($GLOBALS['config']['database']); //mysql_connect('localhost','root','root'); //mysql_query('use shop'); } public function sess_close(){ return true; } //读取session public function sess_read($sess_id){ $time = time(); $expire = ini_get('session.gc_maxlifetime'); $sql="select sess_data from it_session where sess_id='$sess_id' and $time - time < $expire"; if($row=$this->link->fetchRow($sql)){ return $row['sess_data']; }else{ return ''; } } //写入session public function sess_write($sess_id,$sess_data){ $expire=time(); //存在则更新 $sql = "replace into it_session values('$sess_id','$expire', '$sess_data')"; return $this->link->myquery($sql); } //销毁 public function sess_destroy($sess_id){ $sql="delete from it_session where sess_id='$sess_id'"; return $this->link->myquery($sql); } //垃圾回收, 可以设置概率 public function sess_gc($ttl){ $now = time(); $last = $now -$ttl; //删除过期session $sql = "delete from it_session where expire < $last"; return $this->link->myquery($sql); } }