Session.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <?php
  2. /**
  3. * This file is part of workerman.
  4. *
  5. * Licensed under The MIT License
  6. * For full copyright and license information, please see the MIT-LICENSE.txt
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @author walkor<walkor@workerman.net>
  10. * @copyright walkor<walkor@workerman.net>
  11. * @link http://www.workerman.net/
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Workerman\Protocols\Http;
  15. use Workerman\Protocols\Http\Session\SessionHandlerInterface;
  16. /**
  17. * Class Session
  18. * @package Workerman\Protocols\Http
  19. */
  20. class Session
  21. {
  22. /**
  23. * Session andler class which implements SessionHandlerInterface.
  24. *
  25. * @var string
  26. */
  27. protected static $_handlerClass = 'Workerman\Protocols\Http\Session\FileSessionHandler';
  28. /**
  29. * Parameters of __constructor for session handler class.
  30. *
  31. * @var null
  32. */
  33. protected static $_handlerConfig = null;
  34. /**
  35. * Session.gc_probability
  36. *
  37. * @var int
  38. */
  39. protected static $_sessionGcProbability = 1;
  40. /**
  41. * Session.gc_divisor
  42. *
  43. * @var int
  44. */
  45. protected static $_sessionGcDivisor = 1000;
  46. /**
  47. * Session.gc_maxlifetime
  48. *
  49. * @var int
  50. */
  51. protected static $_sessionGcMaxLifeTime = 1440;
  52. /**
  53. * Session handler instance.
  54. *
  55. * @var SessionHandlerInterface
  56. */
  57. protected static $_handler = null;
  58. /**
  59. * Session data.
  60. *
  61. * @var array
  62. */
  63. protected $_data = array();
  64. /**
  65. * Session changed and need to save.
  66. *
  67. * @var bool
  68. */
  69. protected $_needSave = false;
  70. /**
  71. * Session id.
  72. *
  73. * @var null
  74. */
  75. protected $_sessionId = null;
  76. /**
  77. * Session constructor.
  78. *
  79. * @param string $session_id
  80. */
  81. public function __construct($session_id)
  82. {
  83. static::checkSessionId($session_id);
  84. if (static::$_handler === null) {
  85. static::initHandler();
  86. }
  87. $this->_sessionId = $session_id;
  88. if ($data = static::$_handler->read($session_id)) {
  89. $this->_data = \unserialize($data);
  90. }
  91. }
  92. /**
  93. * Get session id.
  94. *
  95. * @return string
  96. */
  97. public function getId()
  98. {
  99. return $this->_sessionId;
  100. }
  101. /**
  102. * Get session.
  103. *
  104. * @param string $name
  105. * @param mixed|null $default
  106. * @return mixed|null
  107. */
  108. public function get($name, $default = null)
  109. {
  110. return isset($this->_data[$name]) ? $this->_data[$name] : $default;
  111. }
  112. /**
  113. * Store data in the session.
  114. *
  115. * @param string $name
  116. * @param mixed $value
  117. */
  118. public function set($name, $value)
  119. {
  120. $this->_data[$name] = $value;
  121. $this->_needSave = true;
  122. }
  123. /**
  124. * Delete an item from the session.
  125. *
  126. * @param string $name
  127. */
  128. public function delete($name)
  129. {
  130. unset($this->_data[$name]);
  131. $this->_needSave = true;
  132. }
  133. /**
  134. * Retrieve and delete an item from the session.
  135. *
  136. * @param string $name
  137. * @param mixed|null $default
  138. * @return mixed|null
  139. */
  140. public function pull($name, $default = null)
  141. {
  142. $value = $this->get($name, $default);
  143. $this->delete($name);
  144. return $value;
  145. }
  146. /**
  147. * Store data in the session.
  148. *
  149. * @param string $key
  150. * @param mixed|null $value
  151. */
  152. public function put($key, $value = null)
  153. {
  154. if (!\is_array($key)) {
  155. $this->set($key, $value);
  156. return;
  157. }
  158. foreach ($key as $k => $v) {
  159. $this->_data[$k] = $v;
  160. }
  161. $this->_needSave = true;
  162. }
  163. /**
  164. * Remove a piece of data from the session.
  165. *
  166. * @param string $name
  167. */
  168. public function forget($name)
  169. {
  170. if (\is_scalar($name)) {
  171. $this->delete($name);
  172. return;
  173. }
  174. if (\is_array($name)) {
  175. foreach ($name as $key) {
  176. unset($this->_data[$key]);
  177. }
  178. }
  179. $this->_needSave = true;
  180. }
  181. /**
  182. * Retrieve all the data in the session.
  183. *
  184. * @return array
  185. */
  186. public function all()
  187. {
  188. return $this->_data;
  189. }
  190. /**
  191. * Remove all data from the session.
  192. *
  193. * @return void
  194. */
  195. public function flush()
  196. {
  197. $this->_needSave = true;
  198. $this->_data = array();
  199. }
  200. /**
  201. * Determining If An Item Exists In The Session.
  202. *
  203. * @param string $name
  204. * @return bool
  205. */
  206. public function has($name)
  207. {
  208. return isset($this->_data[$name]);
  209. }
  210. /**
  211. * To determine if an item is present in the session, even if its value is null.
  212. *
  213. * @param string $name
  214. * @return bool
  215. */
  216. public function exists($name)
  217. {
  218. return \array_key_exists($name, $this->_data);
  219. }
  220. /**
  221. * Save session to store.
  222. *
  223. * @return void
  224. */
  225. public function save()
  226. {
  227. if ($this->_needSave) {
  228. if (empty($this->_data)) {
  229. static::$_handler->destroy($this->_sessionId);
  230. } else {
  231. static::$_handler->write($this->_sessionId, \serialize($this->_data));
  232. }
  233. }
  234. $this->_needSave = false;
  235. }
  236. /**
  237. * Refresh session expire time.
  238. *
  239. * @return bool
  240. */
  241. public function refresh()
  242. {
  243. static::$_handler->updateTimestamp($this->getId());
  244. }
  245. /**
  246. * Init.
  247. *
  248. * @return void
  249. */
  250. public static function init()
  251. {
  252. if ($gc_probability = \ini_get('session.gc_probability')) {
  253. self::$_sessionGcProbability = (int)$gc_probability;
  254. }
  255. if ($gc_divisor = \ini_get('session.gc_divisor')) {
  256. self::$_sessionGcDivisor = (int)$gc_divisor;
  257. }
  258. if ($gc_max_life_time = \ini_get('session.gc_maxlifetime')) {
  259. self::$_sessionGcMaxLifeTime = (int)$gc_max_life_time;
  260. }
  261. }
  262. /**
  263. * Set session handler class.
  264. *
  265. * @param mixed|null $class_name
  266. * @param mixed|null $config
  267. * @return string
  268. */
  269. public static function handlerClass($class_name = null, $config = null)
  270. {
  271. if ($class_name) {
  272. static::$_handlerClass = $class_name;
  273. }
  274. if ($config) {
  275. static::$_handlerConfig = $config;
  276. }
  277. return static::$_handlerClass;
  278. }
  279. /**
  280. * Init handler.
  281. *
  282. * @return void
  283. */
  284. protected static function initHandler()
  285. {
  286. if (static::$_handlerConfig === null) {
  287. static::$_handler = new static::$_handlerClass();
  288. } else {
  289. static::$_handler = new static::$_handlerClass(static::$_handlerConfig);
  290. }
  291. }
  292. /**
  293. * Try GC sessions.
  294. *
  295. * @return void
  296. */
  297. public function tryGcSessions()
  298. {
  299. if (\rand(1, static::$_sessionGcDivisor) > static::$_sessionGcProbability) {
  300. return;
  301. }
  302. static::$_handler->gc(static::$_sessionGcMaxLifeTime);
  303. }
  304. /**
  305. * __destruct.
  306. *
  307. * @return void
  308. */
  309. public function __destruct()
  310. {
  311. $this->save();
  312. $this->tryGcSessions();
  313. }
  314. /**
  315. * Check session id.
  316. *
  317. * @param string $session_id
  318. */
  319. protected static function checkSessionId($session_id)
  320. {
  321. if (!\preg_match('/^[a-zA-Z0-9]+$/', $session_id)) {
  322. throw new SessionException("session_id $session_id is invalid");
  323. }
  324. }
  325. }
  326. /**
  327. * Class SessionException
  328. * @package Workerman\Protocols\Http
  329. */
  330. class SessionException extends \RuntimeException
  331. {
  332. }
  333. // Init session.
  334. Session::init();