FileSessionHandler.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. * Nothing.
  54. *
  55. * @param string $save_path
  56. * @param string $name
  57. * @return bool
  58. */
  59. public function open($save_path, $name)
  60. {
  61. return true;
  62. }
  63. /**
  64. * Reads the session data from the session storage.
  65. *
  66. * @param string $session_id
  67. * @return string
  68. */
  69. public function read($session_id)
  70. {
  71. $session_file = static::sessionFile($session_id);
  72. \clearstatcache();
  73. if (\is_file($session_file)) {
  74. $data = \file_get_contents($session_file);
  75. return $data ? $data : '';
  76. }
  77. return '';
  78. }
  79. /**
  80. * Writes the session data to the session storage.
  81. *
  82. * @param string $session_id
  83. * @param string $session_data
  84. * @return bool
  85. */
  86. public function write($session_id, $session_data)
  87. {
  88. $temp_file = static::$_sessionSavePath.uniqid(mt_rand(), true);
  89. if (!\file_put_contents($temp_file, $session_data)) {
  90. return false;
  91. }
  92. return \rename($temp_file, static::sessionFile($session_id));
  93. }
  94. /**
  95. * Nothing.
  96. *
  97. * @return bool
  98. */
  99. public function close()
  100. {
  101. return true;
  102. }
  103. /**
  104. * Destroys a session.
  105. *
  106. * @param string $session_id
  107. * @return bool
  108. */
  109. public function destroy($session_id)
  110. {
  111. $session_file = static::sessionFile($session_id);
  112. if (\is_file($session_file)) {
  113. \unlink($session_file);
  114. }
  115. return true;
  116. }
  117. /**
  118. * Cleanup old sessions.
  119. *
  120. * @param int $maxlifetime
  121. * @return void
  122. */
  123. public function gc($maxlifetime) {
  124. $time_now = \time();
  125. foreach (\glob(static::$_sessionSavePath . static::$_sessionFilePrefix . '*') as $file) {
  126. if(\is_file($file) && $time_now - \filemtime($file) > $maxlifetime) {
  127. \unlink($file);
  128. }
  129. }
  130. }
  131. /**
  132. * Get session file path.
  133. *
  134. * @param $session_id
  135. * @return string
  136. */
  137. protected static function sessionFile($session_id) {
  138. return static::$_sessionSavePath.static::$_sessionFilePrefix.$session_id;
  139. }
  140. /**
  141. * Get or set session file path.
  142. *
  143. * @param $path
  144. * @return string
  145. */
  146. public static function sessionSavePath($path) {
  147. if ($path) {
  148. if ($path[\strlen($path)-1] !== DIRECTORY_SEPARATOR) {
  149. $path .= DIRECTORY_SEPARATOR;
  150. }
  151. static::$_sessionSavePath = $path;
  152. if (!\is_dir($path)) {
  153. \mkdir($path, 0777, true);
  154. }
  155. }
  156. return $path;
  157. }
  158. }
  159. FileSessionHandler::init();