~~~
<?php
/**
* Created by gather
* Email: chenruiqiang@yd-x.com
* Phone: 16601180687
* Copyright:源動互通(北京)科技有限公司
* Create Time: 2018/5/28 19:12
* 使用Redis管理session
*/
class 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();
~~~
- 簡介
- Cookie
- HTML5 LocalStorage
- session
- 當瀏覽器關閉后,Session就銷毀了嗎?
- mysql數據庫保存session
- HTTP協議的由來
- fsockopen異步請求
- http防盜鏈
- Apache偽靜態知識補充
- 大并發量解決方案
- 大型網站是怎樣解決多用戶高并發訪問
- 網站高并發 大流量訪問的處理及解決方法
- 并發數與在線客戶數?注冊用戶數的關系
- 即時聊天程序
- 反向Ajax實現
- ob緩存作用
- 淺聊并發之戰
- php擴展安裝
- php安裝redis擴展
- SQLMap自動化實施SQL注入共計
- 命名空間namespace
- 集群和分布式之【session共享】
- php Redis存儲Session 【1】
- php Redis存儲Session 【2】
- php mysql存儲session【1】
- php緩存
- 文件緩存
- memcache和redis的比較
- 原生session與session in redis對比
- XSS攻擊【1】
- XSS攻擊【2】
- PHP消息隊列
- php+mysql 模擬發送郵件隊列
- php+mysql 模擬訂單處理隊列
- php+redis 模擬秒殺隊列
- RabbitMQ 消息隊列系統
- beanstalkd
- PHP構建即時通訊
- WebSocket協議
- workerman
- PHP變量的作用域
- PHP傳值和傳引用的區別
- PHP匿名函數
- PHP遞歸函數&應用
- PHP單例模式
- PHP性能優化
- RESTful
- 集群
- 增加pgsql擴展
- php.ini路徑查找
- Swoole Compiler
- mysql 主從
- 主從
- mysql-proxy
- window docker環境