25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

141 lines
2.9 KiB

  1. <?php
  2. namespace Kuxin;
  3. /**
  4. * Class Session
  5. *
  6. * @package Kuxin
  7. * @author Pakey <pakey@qq.com>
  8. */
  9. class Session
  10. {
  11. public static $start = false;
  12. /**
  13. * 启动session
  14. *
  15. * @param array $config
  16. */
  17. public static function start(array $config = [])
  18. {
  19. if (self::$start || session_status() === PHP_SESSION_ACTIVE) {
  20. return;
  21. }
  22. if (!$config) {
  23. $config = Config::get('session', []);
  24. }
  25. if (isset($config['hanlder'])) {
  26. ini_set("session.save_handler", $config['hanlder']);
  27. ini_set("session.save_path", $config['path']);
  28. //ini_set("session.save_path", "tcp://127.0.0.1:11211");
  29. }
  30. session_start();
  31. self::$start = true;
  32. }
  33. /**
  34. * @param string $name
  35. * @param mixed $default
  36. * @return mixed
  37. */
  38. public static function get(string $name = '', $default = null)
  39. {
  40. self::start();
  41. if ($name == '')
  42. return $_SESSION;
  43. //数组模式 找到返回
  44. if (strpos($name, '.')) {
  45. //数组模式 找到返回
  46. $c = $_SESSION;
  47. $fields = explode('.', $name);
  48. foreach ($fields as $field) {
  49. if (!isset($c[$field]))
  50. return (is_callable($default) ? $default($name) : $default);
  51. $c = $c[$field];
  52. }
  53. return $c;
  54. } elseif (isset($_SESSION[$name])) {
  55. return $_SESSION[$name];
  56. } else {
  57. return (is_callable($default) ? $default($name) : $default);
  58. }
  59. }
  60. /**
  61. * 是否存在
  62. * @param string $key
  63. * @return bool
  64. */
  65. public static function has(string $key)
  66. {
  67. self::start();
  68. return isset($_SESSION[$key]);
  69. }
  70. /**
  71. * @param $key
  72. * @param mixed $value
  73. * @return bool
  74. */
  75. public static function set(string $key, $value)
  76. {
  77. $_SESSION[$key] = $value;
  78. return true;
  79. }
  80. /**
  81. * @param $key
  82. * @return bool
  83. */
  84. public static function remove(string $key): bool
  85. {
  86. self::start();
  87. if (!isset($_SESSION[$key])) {
  88. return false;
  89. }
  90. unset($_SESSION[$key]);
  91. return true;
  92. }
  93. /**
  94. * 清空session值
  95. *
  96. * @access public
  97. * @return void
  98. */
  99. public static function clear()
  100. {
  101. $_SESSION = [];
  102. }
  103. /**
  104. * 注销session
  105. *
  106. * @access public
  107. * @return void
  108. */
  109. public static function destory()
  110. {
  111. if (session_id()) {
  112. unset($_SESSION);
  113. session_destroy();
  114. }
  115. }
  116. /**
  117. * 当浏览器关闭时,session将停止写入
  118. *
  119. * @access public
  120. * @return void
  121. */
  122. public static function close()
  123. {
  124. if (session_status() === PHP_SESSION_ACTIVE) {
  125. session_write_close();
  126. }
  127. }
  128. }