FileSessionHandler.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. * {@inheritdoc}
  85. */
  86. public function close()
  87. {
  88. return true;
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function destroy($session_id)
  94. {
  95. $session_file = static::sessionFile($session_id);
  96. if (\is_file($session_file)) {
  97. \unlink($session_file);
  98. }
  99. return true;
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function gc($maxlifetime) {
  105. $time_now = \time();
  106. foreach (\glob(static::$_sessionSavePath . static::$_sessionFilePrefix . '*') as $file) {
  107. if(\is_file($file) && $time_now - \filemtime($file) > $maxlifetime) {
  108. \unlink($file);
  109. }
  110. }
  111. }
  112. /**
  113. * Get session file path.
  114. *
  115. * @param string $session_id
  116. * @return string
  117. */
  118. protected static function sessionFile($session_id) {
  119. return static::$_sessionSavePath.static::$_sessionFilePrefix.$session_id;
  120. }
  121. /**
  122. * Get or set session file path.
  123. *
  124. * @param string $path
  125. * @return string
  126. */
  127. public static function sessionSavePath($path) {
  128. if ($path) {
  129. if ($path[\strlen($path)-1] !== DIRECTORY_SEPARATOR) {
  130. $path .= DIRECTORY_SEPARATOR;
  131. }
  132. static::$_sessionSavePath = $path;
  133. if (!\is_dir($path)) {
  134. \mkdir($path, 0777, true);
  135. }
  136. }
  137. return $path;
  138. }
  139. }
  140. FileSessionHandler::init();