You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
4.8 KiB

7 years ago
  1. <?php
  2. namespace App\Common;
  3. //通过Redis实现Session共享
  4. class RedisSession
  5. {
  6. /**
  7. * 保存session的数据库表的信息
  8. */
  9. private $_options = array(
  10. 'handler' => null, //数据库连接句柄
  11. 'host' => null,
  12. 'port' => null,
  13. 'lifeTime' => null,
  14. 'prefix' => 'PHPREDIS_SESSION:'
  15. );
  16. /**
  17. * 构造函数
  18. * @param $options 设置信息数组
  19. */
  20. public function __construct($options=array())
  21. {
  22. if(!class_exists("redis", false)){
  23. die("必须安装redis扩展");
  24. }
  25. if(!isset($options['lifeTime']) || $options['lifeTime'] <= 0){
  26. $options['lifeTime'] = ini_get('session.gc_maxlifetime');
  27. }
  28. $this->_options = array_merge($this->_options, $options);
  29. }
  30. /**
  31. * 开始使用该驱动的session
  32. */
  33. public function begin()
  34. {
  35. if($this->_options['host'] === null || $this->_options['port'] === null || $this->_options['lifeTime'] === null)
  36. {
  37. return false;
  38. }
  39. //设置session处理函数
  40. session_set_save_handler(
  41. array($this, 'open'),
  42. array($this, 'close'),
  43. array($this, 'read'),
  44. array($this, 'write'),
  45. array($this, 'destory'),
  46. array($this, 'gc')
  47. );
  48. }
  49. /**
  50. * 自动开始回话或者session_start()开始回话后第一个调用的函数
  51. * 类似于构造函数的作用
  52. * @param $savePath 默认的保存路径
  53. * @param $sessionName 默认的参数名,PHPSESSID
  54. */
  55. public function open($savePath, $sessionName)
  56. {
  57. if(is_resource($this->_options['handler'])) return true;
  58. //连接redis
  59. $redisHandle = new Redis();
  60. $redisHandle->connect($this->_options['host'], $this->_options['port']);
  61. if(!$redisHandle){
  62. return false;
  63. }
  64. $this->_options['handler'] = $redisHandle;
  65. // $this->gc(null);
  66. return true;
  67. }
  68. /**
  69. * 类似于析构函数,在write之后调用或者session_write_close()函数之后调用
  70. */
  71. public function close()
  72. {
  73. return $this->_options['handler']->close();
  74. }
  75. /**
  76. * 读取session信息
  77. * @param $sessionId 通过该Id唯一确定对应的session数据
  78. * @return session信息/空串
  79. */
  80. public function read($sessionId)
  81. {
  82. $sessionId = $this->_options['prefix'].$sessionId;
  83. return $this->_options['handler']->get($sessionId);
  84. }
  85. /**
  86. * 写入或者修改session数据
  87. * @param $sessionId 要写入数据的session对应的id
  88. * @param $sessionData 要写入的数据,已经序列化过了
  89. */
  90. public function write($sessionId, $sessionData)
  91. {
  92. $sessionId = $this->_options['prefix'].$sessionId;
  93. return $this->_options['handler']->setex($sessionId, $this->_options['lifeTime'], $sessionData);
  94. }
  95. /**
  96. * 主动销毁session会话
  97. * @param $sessionId 要销毁的会话的唯一id
  98. */
  99. public function destory($sessionId)
  100. {
  101. $sessionId = $this->_options['prefix'].$sessionId;
  102. // $array = $this->print_stack_trace();
  103. // log::write($array);
  104. return $this->_options['handler']->delete($sessionId) >= 1 ? true : false;
  105. }
  106. /**
  107. * 清理绘画中的过期数据
  108. * @param 有效期
  109. */
  110. public function gc($lifeTime)
  111. {
  112. //获取所有sessionid,让过期的释放掉
  113. //$this->_options['handler']->keys("*");
  114. return true;
  115. }
  116. //打印堆栈信息
  117. public function print_stack_trace()
  118. {
  119. $array = debug_backtrace();
  120. //截取用户信息
  121. $var = $this->read(session_id());
  122. $s = strpos($var, "index_dk_user|");
  123. $e = strpos($var, "}authId|");
  124. $user = substr($var,$s+14,$e-13);
  125. $user = unserialize($user);
  126. //print_r($array);//信息很齐全
  127. unset ( $array [0] );
  128. if(!empty($user))
  129. {
  130. $traceInfo = $user['id'].'|'.$user['user_name'].'|'.$user['user_phone'].'|'.$user['presona_name'].'++++++++++++++++\n';
  131. }
  132. else
  133. {
  134. $traceInfo = '++++++++++++++++\n';
  135. }
  136. $time = date ( "y-m-d H:i:m" );
  137. foreach ( $array as $t )
  138. {
  139. $traceInfo .= '[' . $time . '] ' . $t ['file'] . ' (' . $t ['line'] . ') ';
  140. $traceInfo .= $t ['class'] . $t ['type'] . $t ['function'] . '(';
  141. $traceInfo .= implode ( ', ', $t ['args'] );
  142. $traceInfo .= ")\n";
  143. }
  144. $traceInfo .= '++++++++++++++++';
  145. return $traceInfo;
  146. }
  147. }
  148. //-------------------------------------------
  149. //示例
  150. //入口处调用
  151. /* $handler = new redisSession(array(
  152. 'host' => "127.0.0.1",
  153. 'port' => "6379"
  154. ));
  155. $handler->begin(); */