Session.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think;
  12. use think\exception\ClassNotFoundException;
  13. class Session
  14. {
  15. protected static $prefix = '';
  16. protected static $init = null;
  17. /**
  18. * 设置或者获取session作用域(前缀)
  19. * @param string $prefix
  20. * @return string|void
  21. */
  22. public static function prefix($prefix = '')
  23. {
  24. empty(self::$init) && self::boot();
  25. if (empty($prefix) && null !== $prefix) {
  26. return self::$prefix;
  27. } else {
  28. self::$prefix = $prefix;
  29. }
  30. }
  31. /**
  32. * session初始化
  33. * @param array $config
  34. * @return void
  35. * @throws \think\Exception
  36. */
  37. public static function init(array $config = [])
  38. {
  39. if (empty($config)) {
  40. $config = Config::get('session');
  41. }
  42. // 记录初始化信息
  43. App::$debug && Log::record('[ SESSION ] INIT ' . var_export($config, true), 'info');
  44. $isDoStart = false;
  45. if (isset($config['use_trans_sid'])) {
  46. ini_set('session.use_trans_sid', $config['use_trans_sid'] ? 1 : 0);
  47. }
  48. // 启动session
  49. if (!empty($config['auto_start']) && PHP_SESSION_ACTIVE != session_status()) {
  50. ini_set('session.auto_start', 0);
  51. $isDoStart = true;
  52. }
  53. if (isset($config['prefix']) && ('' === self::$prefix || null === self::$prefix)) {
  54. self::$prefix = $config['prefix'];
  55. }
  56. if (isset($config['var_session_id']) && isset($_REQUEST[$config['var_session_id']])) {
  57. session_id($_REQUEST[$config['var_session_id']]);
  58. } elseif (isset($config['id']) && !empty($config['id'])) {
  59. session_id($config['id']);
  60. }
  61. if (isset($config['name'])) {
  62. session_name($config['name']);
  63. }
  64. if (isset($config['path'])) {
  65. session_save_path($config['path']);
  66. }
  67. if (isset($config['domain'])) {
  68. ini_set('session.cookie_domain', $config['domain']);
  69. }
  70. if (isset($config['expire'])) {
  71. ini_set('session.gc_maxlifetime', $config['expire']);
  72. ini_set('session.cookie_lifetime', $config['expire']);
  73. }
  74. if (isset($config['secure'])) {
  75. ini_set('session.cookie_secure', $config['secure']);
  76. }
  77. if (isset($config['httponly'])) {
  78. ini_set('session.cookie_httponly', $config['httponly']);
  79. }
  80. if (isset($config['use_cookies'])) {
  81. ini_set('session.use_cookies', $config['use_cookies'] ? 1 : 0);
  82. }
  83. if (isset($config['cache_limiter'])) {
  84. session_cache_limiter($config['cache_limiter']);
  85. }
  86. if (isset($config['cache_expire'])) {
  87. session_cache_expire($config['cache_expire']);
  88. }
  89. if (!empty($config['type'])) {
  90. // 读取session驱动
  91. $class = false !== strpos($config['type'], '\\') ? $config['type'] : '\\think\\session\\driver\\' . ucwords($config['type']);
  92. // 检查驱动类
  93. if (!class_exists($class) || !session_set_save_handler(new $class($config))) {
  94. throw new ClassNotFoundException('error session handler:' . $class, $class);
  95. }
  96. }
  97. if ($isDoStart) {
  98. session_start();
  99. self::$init = true;
  100. } else {
  101. self::$init = false;
  102. }
  103. }
  104. /**
  105. * session自动启动或者初始化
  106. * @return void
  107. */
  108. public static function boot()
  109. {
  110. if (is_null(self::$init)) {
  111. self::init();
  112. } elseif (false === self::$init) {
  113. if (PHP_SESSION_ACTIVE != session_status()) {
  114. session_start();
  115. }
  116. self::$init = true;
  117. }
  118. }
  119. /**
  120. * session设置
  121. * @param string $name session名称
  122. * @param mixed $value session值
  123. * @param string|null $prefix 作用域(前缀)
  124. * @return void
  125. */
  126. public static function set($name, $value = '', $prefix = null)
  127. {
  128. empty(self::$init) && self::boot();
  129. $prefix = !is_null($prefix) ? $prefix : self::$prefix;
  130. if (strpos($name, '.')) {
  131. // 二维数组赋值
  132. list($name1, $name2) = explode('.', $name);
  133. if ($prefix) {
  134. $_SESSION[$prefix][$name1][$name2] = $value;
  135. } else {
  136. $_SESSION[$name1][$name2] = $value;
  137. }
  138. } elseif ($prefix) {
  139. $_SESSION[$prefix][$name] = $value;
  140. } else {
  141. $_SESSION[$name] = $value;
  142. }
  143. }
  144. /**
  145. * session获取
  146. * @param string $name session名称
  147. * @param string|null $prefix 作用域(前缀)
  148. * @return mixed
  149. */
  150. public static function get($name = '', $prefix = null)
  151. {
  152. empty(self::$init) && self::boot();
  153. $prefix = !is_null($prefix) ? $prefix : self::$prefix;
  154. if ('' == $name) {
  155. // 获取全部的session
  156. $value = $prefix ? (!empty($_SESSION[$prefix]) ? $_SESSION[$prefix] : []) : $_SESSION;
  157. } elseif ($prefix) {
  158. // 获取session
  159. if (strpos($name, '.')) {
  160. list($name1, $name2) = explode('.', $name);
  161. $value = isset($_SESSION[$prefix][$name1][$name2]) ? $_SESSION[$prefix][$name1][$name2] : null;
  162. } else {
  163. $value = isset($_SESSION[$prefix][$name]) ? $_SESSION[$prefix][$name] : null;
  164. }
  165. } else {
  166. if (strpos($name, '.')) {
  167. list($name1, $name2) = explode('.', $name);
  168. $value = isset($_SESSION[$name1][$name2]) ? $_SESSION[$name1][$name2] : null;
  169. } else {
  170. $value = isset($_SESSION[$name]) ? $_SESSION[$name] : null;
  171. }
  172. }
  173. return $value;
  174. }
  175. /**
  176. * session获取并删除
  177. * @param string $name session名称
  178. * @param string|null $prefix 作用域(前缀)
  179. * @return mixed
  180. */
  181. public static function pull($name, $prefix = null)
  182. {
  183. $result = self::get($name, $prefix);
  184. if ($result) {
  185. self::delete($name, $prefix);
  186. return $result;
  187. } else {
  188. return;
  189. }
  190. }
  191. /**
  192. * session设置 下一次请求有效
  193. * @param string $name session名称
  194. * @param mixed $value session值
  195. * @param string|null $prefix 作用域(前缀)
  196. * @return void
  197. */
  198. public static function flash($name, $value)
  199. {
  200. self::set($name, $value);
  201. if (!self::has('__flash__.__time__')) {
  202. self::set('__flash__.__time__', $_SERVER['REQUEST_TIME_FLOAT']);
  203. }
  204. self::push('__flash__', $name);
  205. }
  206. /**
  207. * 清空当前请求的session数据
  208. * @return void
  209. */
  210. public static function flush()
  211. {
  212. if (self::$init) {
  213. $item = self::get('__flash__');
  214. if (!empty($item)) {
  215. $time = $item['__time__'];
  216. if ($_SERVER['REQUEST_TIME_FLOAT'] > $time) {
  217. unset($item['__time__']);
  218. self::delete($item);
  219. self::set('__flash__', []);
  220. }
  221. }
  222. }
  223. }
  224. /**
  225. * 删除session数据
  226. * @param string|array $name session名称
  227. * @param string|null $prefix 作用域(前缀)
  228. * @return void
  229. */
  230. public static function delete($name, $prefix = null)
  231. {
  232. empty(self::$init) && self::boot();
  233. $prefix = !is_null($prefix) ? $prefix : self::$prefix;
  234. if (is_array($name)) {
  235. foreach ($name as $key) {
  236. self::delete($key, $prefix);
  237. }
  238. } elseif (strpos($name, '.')) {
  239. list($name1, $name2) = explode('.', $name);
  240. if ($prefix) {
  241. unset($_SESSION[$prefix][$name1][$name2]);
  242. } else {
  243. unset($_SESSION[$name1][$name2]);
  244. }
  245. } else {
  246. if ($prefix) {
  247. unset($_SESSION[$prefix][$name]);
  248. } else {
  249. unset($_SESSION[$name]);
  250. }
  251. }
  252. }
  253. /**
  254. * 清空session数据
  255. * @param string|null $prefix 作用域(前缀)
  256. * @return void
  257. */
  258. public static function clear($prefix = null)
  259. {
  260. empty(self::$init) && self::boot();
  261. $prefix = !is_null($prefix) ? $prefix : self::$prefix;
  262. if ($prefix) {
  263. unset($_SESSION[$prefix]);
  264. } else {
  265. $_SESSION = [];
  266. }
  267. }
  268. /**
  269. * 判断session数据
  270. * @param string $name session名称
  271. * @param string|null $prefix
  272. * @return bool
  273. */
  274. public static function has($name, $prefix = null)
  275. {
  276. empty(self::$init) && self::boot();
  277. $prefix = !is_null($prefix) ? $prefix : self::$prefix;
  278. if (strpos($name, '.')) {
  279. // 支持数组
  280. list($name1, $name2) = explode('.', $name);
  281. return $prefix ? isset($_SESSION[$prefix][$name1][$name2]) : isset($_SESSION[$name1][$name2]);
  282. } else {
  283. return $prefix ? isset($_SESSION[$prefix][$name]) : isset($_SESSION[$name]);
  284. }
  285. }
  286. /**
  287. * 添加数据到一个session数组
  288. * @param string $key
  289. * @param mixed $value
  290. * @return void
  291. */
  292. public static function push($key, $value)
  293. {
  294. $array = self::get($key);
  295. if (is_null($array)) {
  296. $array = [];
  297. }
  298. $array[] = $value;
  299. self::set($key, $array);
  300. }
  301. /**
  302. * 启动session
  303. * @return void
  304. */
  305. public static function start()
  306. {
  307. session_start();
  308. self::$init = true;
  309. }
  310. /**
  311. * 销毁session
  312. * @return void
  313. */
  314. public static function destroy()
  315. {
  316. if (!empty($_SESSION)) {
  317. $_SESSION = [];
  318. }
  319. session_unset();
  320. session_destroy();
  321. self::$init = null;
  322. }
  323. /**
  324. * 重新生成session_id
  325. * @param bool $delete 是否删除关联会话文件
  326. * @return void
  327. */
  328. public static function regenerate($delete = false)
  329. {
  330. session_regenerate_id($delete);
  331. }
  332. /**
  333. * 暂停session
  334. * @return void
  335. */
  336. public static function pause()
  337. {
  338. // 暂停session
  339. session_write_close();
  340. self::$init = false;
  341. }
  342. }