Session.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace Heanup\Frame;
  3. class Session
  4. {
  5. protected static $expire = ADMIN_TOKEN_EXPIRED;
  6. /**
  7. * 设置超时时间, 初始化 cookie等
  8. *
  9. * @param int $expire
  10. * @throws \Exception
  11. */
  12. public static function start($expire = 1800)
  13. {
  14. if (is_numeric($expire)) {
  15. self::$expire = $expire;
  16. }
  17. self::getSessionId();
  18. }
  19. /**
  20. * 设置会话信息
  21. *
  22. * @param string $key
  23. * @param string $data
  24. * @param null $user_id
  25. * @return bool
  26. */
  27. public static function set($key, $data, $user_id = null)
  28. {
  29. $sessionId = self::getSessionId($user_id);
  30. try {
  31. $redis = self::getConnection(true);
  32. $isExisted = $redis->exists($sessionId);
  33. $result = $redis->hSet($sessionId, $key, $data);
  34. // if (!$isExisted) {
  35. $redis->expire($sessionId, self::$expire);
  36. // }
  37. } catch (\Exception $ex) {
  38. Logger::debug($ex, 'session.set');
  39. $result = false;
  40. }
  41. return $result;
  42. }
  43. /**
  44. * 读取会话信息
  45. *
  46. * @param $key
  47. * @param null $user_id
  48. * @return mixed
  49. */
  50. public static function get($key, $user_id = null)
  51. {
  52. $sessionId = self::getSessionId($user_id);
  53. try {
  54. $redis = self::getConnection();
  55. $result = $redis->hGet($sessionId, $key);
  56. $redis->expire($sessionId, self::$expire);
  57. } catch (\Exception $ex) {
  58. Logger::debug($ex, 'session.get');
  59. $result = null;
  60. }
  61. return $result;
  62. }
  63. /**
  64. * 获取所有的会话信息
  65. *
  66. * @param null $user_id
  67. * @return array
  68. */
  69. public static function getAll($user_id = null)
  70. {
  71. $sessionId = self::getSessionId($user_id);
  72. try {
  73. $result = self::getConnection()->hGetAll($sessionId);
  74. } catch (\Exception $ex) {
  75. Logger::debug($ex, 'session.get_all');
  76. $result = array();
  77. }
  78. return $result;
  79. }
  80. /**
  81. * 删除会话
  82. *
  83. * @param null $user_id
  84. * @return bool
  85. */
  86. public static function destroy($user_id = null)
  87. {
  88. $sessionId = self::getSessionId($user_id);
  89. try {
  90. $result = self::getConnection(true)->delete($sessionId);
  91. $_COOKIE[$sessionId] = null;
  92. } catch (\Exception $ex) {
  93. Logger::debug($ex, 'session.destroy');
  94. $result = false;
  95. }
  96. return $result;
  97. }
  98. /**
  99. * 获取会话ID
  100. *
  101. * @param null $user_id
  102. * @return string
  103. * @throws \Exception
  104. */
  105. public static function getSessionId($user_id = null)
  106. {
  107. if ($user_id) {
  108. $sessionId = md5($user_id);
  109. } else {
  110. $host = $_SERVER['HTTP_HOST'];
  111. $key = 'sid:' . crc32($host);
  112. if (!$_COOKIE[$key]) {
  113. $sessionId = md5(microtime() . rand() . uniqid());
  114. $result = setcookie($key, $sessionId, time() + self::$expire * 10, '/', $host, null, true);
  115. if (!$result) {
  116. throw new \Exception('set cookie failed', 9000);
  117. }
  118. } else {
  119. $sessionId = $_COOKIE[$key];
  120. }
  121. }
  122. return $sessionId;
  123. }
  124. /**
  125. *
  126. * @param bool $isMaster
  127. * @return \Redis
  128. * @internal param $ $isMaster* $isMaster
  129. */
  130. protected static function getConnection($isMaster = false)
  131. {
  132. $configClass = 'Redis\Session';
  133. $redis = new \Heanup\Frame\Library\Redis($configClass);
  134. $connection = $redis->getConnection($isMaster);
  135. return $connection;
  136. }
  137. }