FileSessionHandler.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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\Session;
  15. /**
  16. * Class FileSessionHandler
  17. * @package Workerman\Protocols\Http\Session
  18. */
  19. class FileSessionHandler implements SessionHandlerInterface
  20. {
  21. /**
  22. * Session save path.
  23. *
  24. * @var string
  25. */
  26. protected static $_sessionSavePath = null;
  27. /**
  28. * Session file prefix.
  29. *
  30. * @var string
  31. */
  32. protected static $_sessionFilePrefix = 'session_';
  33. /**
  34. * Init.
  35. */
  36. public static function init() {
  37. $save_path = @\session_save_path();
  38. if (!$save_path || \strpos($save_path, 'tcp://') === 0) {
  39. $save_path = \sys_get_temp_dir();
  40. }
  41. static::sessionSavePath($save_path);
  42. }
  43. /**
  44. * FileSessionHandler constructor.
  45. * @param array $config
  46. */
  47. public function __construct($config = array()) {
  48. if (isset($config['save_path'])) {
  49. static::sessionSavePath($config['save_path']);
  50. }
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function open($save_path, $name)
  56. {
  57. return true;
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function read($session_id)
  63. {
  64. $session_file = static::sessionFile($session_id);
  65. \clearstatcache();
  66. if (\is_file($session_file)) {
  67. $data = \file_get_contents($session_file);
  68. return $data ? $data : '';
  69. }
  70. return '';
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function write($session_id, $session_data)
  76. {
  77. $temp_file = static::$_sessionSavePath.uniqid(mt_rand(), true);
  78. if (!\file_put_contents($temp_file, $session_data)) {
  79. return false;
  80. }
  81. return \rename($temp_file, static::sessionFile($session_id));
  82. }
  83. /**
  84. * Update sesstion modify time.
  85. *
  86. * @see https://www.php.net/manual/en/class.sessionupdatetimestamphandlerinterface.php
  87. * @see https://www.php.net/manual/zh/function.touch.php
  88. *
  89. * @param string $id Session id.
  90. * @param string $data Session Data.
  91. *
  92. * @return bool
  93. */
  94. public function updateTimestamp($id, $data = "")
  95. {
  96. $session_file = static::sessionFile($id);
  97. if (!file_exists($session_file)) {
  98. return false;
  99. }
  100. // set file modify time to current time
  101. $set_modify_time = \touch($session_file);
  102. // clear file stat cache
  103. \clearstatcache();
  104. return $set_modify_time;
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function close()
  110. {
  111. return true;
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function destroy($session_id)
  117. {
  118. $session_file = static::sessionFile($session_id);
  119. if (\is_file($session_file)) {
  120. \unlink($session_file);
  121. }
  122. return true;
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function gc($maxlifetime) {
  128. $time_now = \time();
  129. foreach (\glob(static::$_sessionSavePath . static::$_sessionFilePrefix . '*') as $file) {
  130. if(\is_file($file) && $time_now - \filemtime($file) > $maxlifetime) {
  131. \unlink($file);
  132. }
  133. }
  134. }
  135. /**
  136. * Get session file path.
  137. *
  138. * @param string $session_id
  139. * @return string
  140. */
  141. protected static function sessionFile($session_id) {
  142. return static::$_sessionSavePath.static::$_sessionFilePrefix.$session_id;
  143. }
  144. /**
  145. * Get or set session file path.
  146. *
  147. * @param string $path
  148. * @return string
  149. */
  150. public static function sessionSavePath($path) {
  151. if ($path) {
  152. if ($path[\strlen($path)-1] !== DIRECTORY_SEPARATOR) {
  153. $path .= DIRECTORY_SEPARATOR;
  154. }
  155. static::$_sessionSavePath = $path;
  156. if (!\is_dir($path)) {
  157. \mkdir($path, 0777, true);
  158. }
  159. }
  160. return $path;
  161. }
  162. }
  163. FileSessionHandler::init();