/src/core/Session.php

https://github.com/swoole/framework · PHP · 198 lines · 121 code · 14 blank · 63 comment · 16 complexity · dce4a3571cbb1c63016a46e7d21d97d4 MD5 · raw file

  1. <?php
  2. namespace SPF;
  3. use RuntimeException;
  4. /**
  5. * 会话控制类
  6. * 通过SPF\Cache系统实现会话控制,可支持FileCache, DBCache, Memcache以及更多
  7. * @author Tianfeng.Han
  8. * @package SPF
  9. */
  10. class Session
  11. {
  12. protected $config;
  13. // 类成员属性定义
  14. static $cache_prefix = "phpsess_";
  15. static $cookie_key = 'PHPSESSID';
  16. static $sess_size = 32;
  17. /**
  18. * 是否启动
  19. * @var bool
  20. */
  21. public $isStart = false;
  22. protected $sessID;
  23. protected $readonly; //是否为只读,只读不需要保存
  24. /**
  25. * @var IFace\Cache
  26. */
  27. protected $cache;
  28. /**
  29. * 使用PHP内建的SESSION
  30. * @var bool
  31. */
  32. public $use_php_session = true;
  33. protected $cookie_lifetime = 86400000;
  34. protected $session_lifetime = 0;
  35. protected $cookie_domain = null;
  36. protected $cookie_path = '/';
  37. public function __construct($config)
  38. {
  39. $this->config = $config;
  40. /**
  41. * 使用PHP提供的Session
  42. */
  43. if (isset($config['use_php_session']) and $config['use_php_session'])
  44. {
  45. $this->use_php_session = true;
  46. return;
  47. }
  48. if (isset($config['cache_id']) && $config['cache_id'])
  49. {
  50. $this->cache = Factory::getCache($config['cache_id']);
  51. }
  52. /**
  53. * cookie过期时间
  54. */
  55. if (isset($config['cookie_lifetime']))
  56. {
  57. $this->cookie_lifetime = intval($config['cookie_lifetime']);
  58. }
  59. if (isset($config))
  60. /**
  61. * cookie的路径
  62. */
  63. if (isset($config['cookie_path']))
  64. {
  65. $this->cookie_path = $config['cookie_path'];
  66. }
  67. /**
  68. * cookie域名
  69. */
  70. if (isset($config['cookie_domain']))
  71. {
  72. $this->cookie_domain = $config['cookie_domain'];
  73. }
  74. /**
  75. * session的过期时间
  76. */
  77. if (isset($config['session_lifetime']))
  78. {
  79. $this->session_lifetime = intval($config['session_lifetime']);
  80. }
  81. $this->use_php_session = false;
  82. /**
  83. * 注册钩子,请求结束后保存Session
  84. */
  85. App::getInstance()->afterRequest(array($this, 'save'));
  86. }
  87. /**
  88. * 启动会话
  89. * @param bool $readonly
  90. * @throws RuntimeException
  91. */
  92. public function start($readonly = false)
  93. {
  94. if (empty(App::getInstance()->request))
  95. {
  96. throw new RuntimeException("The method must be used when requested.");
  97. }
  98. $this->isStart = true;
  99. if ($this->use_php_session)
  100. {
  101. session_start();
  102. }
  103. else
  104. {
  105. $this->readonly = $readonly;
  106. $sessid = $this->sessID;
  107. if (empty($sessid)){
  108. $sessid = Cookie::get(self::$cookie_key);
  109. if (empty($sessid))
  110. {
  111. $sessid = RandomKey::randmd5(40);
  112. App::getInstance()->http->setCookie(self::$cookie_key, $sessid, time() + $this->cookie_lifetime,
  113. $this->cookie_path, $this->cookie_domain);
  114. }
  115. }
  116. $_SESSION = $this->load($sessid);
  117. }
  118. App::getInstance()->request->session = $_SESSION;
  119. }
  120. /**
  121. * 设置SessionID
  122. * @param $session_id
  123. */
  124. function setId($session_id)
  125. {
  126. $this->sessID = $session_id;
  127. if ($this->use_php_session)
  128. {
  129. session_id($session_id);
  130. }
  131. }
  132. /**
  133. * 获取SessionID
  134. * @return string
  135. */
  136. function getId()
  137. {
  138. if ($this->use_php_session)
  139. {
  140. return session_id();
  141. }
  142. else
  143. {
  144. return $this->sessID;
  145. }
  146. }
  147. /**
  148. * 加载Session
  149. * @param $sessId
  150. * @return array
  151. */
  152. public function load($sessId)
  153. {
  154. $this->sessID = $sessId;
  155. $data = $this->cache->get(self::$cache_prefix . $sessId);
  156. //先读数据,如果没有,就初始化一个
  157. if (!empty($data))
  158. {
  159. return unserialize($data);
  160. }
  161. else
  162. {
  163. return array();
  164. }
  165. }
  166. /**
  167. * 保存Session
  168. * @return bool
  169. */
  170. public function save()
  171. {
  172. /**
  173. * 使用PHP Sesion,Readonl,未启动 这3种情况下不需要保存
  174. */
  175. if ($this->use_php_session or !$this->isStart or $this->readonly)
  176. {
  177. return true;
  178. }
  179. //设置为Session关闭状态
  180. $this->isStart = false;
  181. $key = self::$cache_prefix . $this->sessID;
  182. return $this->cache->set($key, serialize($_SESSION), $this->session_lifetime);
  183. }
  184. }