Session.php 3.3 KB

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