標籤:ret 構造 time port substr inf null index sts
<?phpclass redisSession{ /** * 儲存session的資料庫表的資訊 */ private $_options = array( ‘handler‘ => null, //資料庫連接控制代碼 ‘host‘ => null, ‘port‘ => null, ‘lifeTime‘ => null, ‘prefix‘ => ‘PHPREDIS_SESSION:‘ ); /** * 建構函式 * @param $options 設定資訊數組 */ public function __construct($options=array()){ if(!class_exists("redis", false)){ die("必須安裝redis擴充"); } if(!isset($options[‘lifeTime‘]) || $options[‘lifeTime‘] <= 0){ $options[‘lifeTime‘] = ini_get(‘session.gc_maxlifetime‘); } $this->_options = array_merge($this->_options, $options); } /** * 開始使用該驅動的session */ public function begin(){ if($this->_options[‘host‘] === null || $this->_options[‘port‘] === null || $this->_options[‘lifeTime‘] === null ){ return false; } //設定session處理函數 session_set_save_handler( array($this, ‘open‘), array($this, ‘close‘), array($this, ‘read‘), array($this, ‘write‘), array($this, ‘destory‘), array($this, ‘gc‘) ); } /** * 自動開始回話或者session_start()開始回話後第一個調用的函數 * 類似於建構函式的作用 * @param $savePath 預設的儲存路徑 * @param $sessionName 預設的參數名,PHPSESSID */ public function open($savePath, $sessionName){ if(is_resource($this->_options[‘handler‘])) return true; //串連redis $redisHandle = new Redis(); $redisHandle->connect($this->_options[‘host‘], $this->_options[‘port‘]); if(!$redisHandle){ return false; } $this->_options[‘handler‘] = $redisHandle;// $this->gc(null); return true; } /** * 類似於解構函式,在write之後調用或者session_write_close()函數之後調用 */ public function close(){ return $this->_options[‘handler‘]->close(); } /** * 讀取session資訊 * @param $sessionId 通過該Id唯一確定對應的session資料 * @return session資訊/空串 */ public function read($sessionId){ $sessionId = $this->_options[‘prefix‘].$sessionId; return $this->_options[‘handler‘]->get($sessionId); } /** * 寫入或者修改session資料 * @param $sessionId 要寫入資料的session對應的id * @param $sessionData 要寫入的資料,已經序列化過了 */ public function write($sessionId, $sessionData){ $sessionId = $this->_options[‘prefix‘].$sessionId; return $this->_options[‘handler‘]->setex($sessionId, $this->_options[‘lifeTime‘], $sessionData); } /** * 主動銷毀session會話 * @param $sessionId 要銷毀的會話的唯一id */ public function destory($sessionId){ $sessionId = $this->_options[‘prefix‘].$sessionId; // $array = $this->print_stack_trace();// log::write($array); return $this->_options[‘handler‘]->delete($sessionId) >= 1 ? true : false; } /** * 清理繪畫中的到期資料 * @param 有效期間 */ public function gc($lifeTime){ //擷取所有sessionid,讓到期的釋放掉 //$this->_options[‘handler‘]->keys("*"); return true; } //列印堆棧資訊 public function print_stack_trace() { $array = debug_backtrace (); //截取使用者資訊 $var = $this->read(session_id()); $s = strpos($var, "index_dk_user|"); $e = strpos($var, "}authId|"); $user = substr($var,$s+14,$e-13); $user = unserialize($user); //print_r($array);//資訊很齊全 unset ( $array [0] ); if(!empty($user)){ $traceInfo = $user[‘id‘].‘|‘.$user[‘user_name‘].‘|‘.$user[‘user_phone‘].‘|‘.$user[‘presona_name‘].‘++++++++++++++++\n‘; }else{ $traceInfo = ‘++++++++++++++++\n‘; } $time = date ( "y-m-d H:i:m" ); foreach ( $array as $t ) { $traceInfo .= ‘[‘ . $time . ‘] ‘ . $t [‘file‘] . ‘ (‘ . $t [‘line‘] . ‘) ‘; $traceInfo .= $t [‘class‘] . $t [‘type‘] . $t [‘function‘] . ‘(‘; $traceInfo .= implode ( ‘, ‘, $t [‘args‘] ); $traceInfo .= ")\n"; } $traceInfo .= ‘++++++++++++++++‘; return $traceInfo; }}
入口處調用
$handler = new redisSession(array( ‘host‘ => "127.0.0.1", ‘port‘ => "6379" ));$handler->begin();
通過redis實現session共用-php