redis = new Redis; $this->enable = $this->redis->connect('127.0.0.1', 6379); } public function init() { // @ini_set("session.save_handler", "redis"); // session_save_path(self::save_path); session_set_save_handler( array(&$this,'onOpen'), array(&$this,'onClose'), array(&$this,'onRead'), array(&$this,'onWrite'), array(&$this,'onDestroy'), array(&$this,'onGc')); //array(&$this,'onCreateSid')); } public function start() { //会触发,open 和 read 函数 session_start(); } public function end() { // 会触发write 和 close 函数 if($this->fdestroy == false) { session_write_close(); } foreach($_SESSION as $key=>$value) { unset($_SESSION[$key]); } } public function destroy() { $this->fdestroy = true; session_destroy();//会触发destroy 和 close 函数 } public function onOpen() { Log::record("onOpen",Log::DEBUG); return true; } public function onRead($sid) { Log::record("onRead sid={$sid}",Log::DEBUG); $ret = $this->redis->hGet(self::prefix,$sid); if(empty($ret)) { Log::record("onRead " . $_COOKIE[self::session_name],Log::DEBUG); if(empty($_COOKIE[self::session_name]) || $_COOKIE[self::session_name] != $sid) { Log::record("onRead fcgi_setcookie reset cookie",Log::DEBUG); fcgi_setcookie("PHPSESSID","{$sid}",time() + self::sid_expire); } return ''; } else { Log::record("onRead 2",Log::DEBUG); return $ret[0]; } } public function onClose() { Log::record("onClose",Log::DEBUG); return true; } public function onWrite($sid, $data) { Log::record("onWrite sid={$sid}",Log::DEBUG); $ret = $this->redis->hSet(self::prefix,$sid,$data); if($ret) { $ret = $this->redis->expire(self::prefix . ":" . $sid, self::sid_expire); } return true; } public function onDestroy($sid) { Log::record("onDestroy sid={$sid}",Log::DEBUG); return dcache($sid,self::prefix); } public function onGc($expire) { Log::record("onGc expire={$expire}",Log::DEBUG); return true; } // public function onCreateSid() // { // $sid = 'vnk730je4l1c20cc479gpoesm6'; // // return $sid; // } }