Нема описа
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. // 目录不存在就创建 by 小虎哥 2018-05-07
  67. if (!empty($config['path']) && !file_exists($config['path'])) {
  68. tp_mkdir($config['path']);
  69. }
  70. if (!empty($config['path'])) {
  71. session_save_path($config['path']);
  72. }
  73. // --------end
  74. }
  75. if (isset($config['domain'])) {
  76. ini_set('session.cookie_domain', $config['domain']);
  77. }
  78. if (isset($config['expire'])) {
  79. // 这个是Session数据在服务器端储存的时间,如果超过这个时间,那么Session数据就自动删除
  80. ini_set('session.gc_maxlifetime', $config['expire']);
  81. }
  82. if (isset($config['cookie_expire'])) {
  83. // 这个代表SessionID在客户端Cookie储存的时间,默认是0,代表浏览器一关闭SessionID就作废
  84. ini_set('session.cookie_lifetime', $config['cookie_expire']);
  85. }
  86. if (isset($config['secure'])) {
  87. ini_set('session.cookie_secure', $config['secure']);
  88. }
  89. if (isset($config['httponly'])) {
  90. ini_set('session.cookie_httponly', $config['httponly']);
  91. }
  92. if (isset($config['use_cookies'])) {
  93. ini_set('session.use_cookies', $config['use_cookies'] ? 1 : 0);
  94. }
  95. if (isset($config['cache_limiter'])) {
  96. session_cache_limiter($config['cache_limiter']);
  97. }
  98. if (isset($config['cache_expire'])) {
  99. session_cache_expire($config['cache_expire']);
  100. }
  101. if (!empty($config['type'])) {
  102. // 读取session驱动
  103. $class = false !== strpos($config['type'], '\\') ? $config['type'] : '\\think\\session\\driver\\' . ucwords($config['type']);
  104. // 检查驱动类
  105. if (!class_exists($class) || !session_set_save_handler(new $class($config))) {
  106. throw new ClassNotFoundException('error session handler:' . $class, $class);
  107. }
  108. }
  109. if ($isDoStart) {
  110. session_start();
  111. self::$init = true;
  112. } else {
  113. self::$init = false;
  114. }
  115. }
  116. /**
  117. * session自动启动或者初始化
  118. * @return void
  119. */
  120. public static function boot()
  121. {
  122. if (is_null(self::$init)) {
  123. self::init();
  124. } elseif (false === self::$init) {
  125. if (PHP_SESSION_ACTIVE != session_status()) {
  126. session_start();
  127. }
  128. self::$init = true;
  129. }
  130. }
  131. /**
  132. * session设置
  133. * @param string $name session名称
  134. * @param mixed $value session值
  135. * @param string|null $prefix 作用域(前缀)
  136. * @return void
  137. */
  138. public static function set($name, $value = '', $prefix = null)
  139. {
  140. empty(self::$init) && self::boot();
  141. $prefix = !is_null($prefix) ? $prefix : self::$prefix;
  142. if (strpos($name, '.')) {
  143. // 二维数组赋值
  144. list($name1, $name2) = explode('.', $name);
  145. if ($prefix) {
  146. $_SESSION[$prefix][$name1][$name2] = $value;
  147. } else {
  148. $_SESSION[$name1][$name2] = $value;
  149. }
  150. } elseif ($prefix) {
  151. $_SESSION[$prefix][$name] = $value;
  152. } else {
  153. $_SESSION[$name] = $value;
  154. }
  155. }
  156. /**
  157. * session获取
  158. * @param string $name session名称
  159. * @param string|null $prefix 作用域(前缀)
  160. * @return mixed
  161. */
  162. public static function get($name = '', $prefix = null)
  163. {
  164. empty(self::$init) && self::boot();
  165. $prefix = !is_null($prefix) ? $prefix : self::$prefix;
  166. if ('' == $name) {
  167. // 获取全部的session
  168. $value = $prefix ? (!empty($_SESSION[$prefix]) ? $_SESSION[$prefix] : []) : $_SESSION;
  169. } elseif ($prefix) {
  170. // 获取session
  171. if (strpos($name, '.')) {
  172. list($name1, $name2) = explode('.', $name);
  173. $value = isset($_SESSION[$prefix][$name1][$name2]) ? $_SESSION[$prefix][$name1][$name2] : null;
  174. } else {
  175. $value = isset($_SESSION[$prefix][$name]) ? $_SESSION[$prefix][$name] : null;
  176. }
  177. } else {
  178. if (strpos($name, '.')) {
  179. list($name1, $name2) = explode('.', $name);
  180. $value = isset($_SESSION[$name1][$name2]) ? $_SESSION[$name1][$name2] : null;
  181. } else {
  182. $value = isset($_SESSION[$name]) ? $_SESSION[$name] : null;
  183. }
  184. }
  185. return $value;
  186. }
  187. /**
  188. * session获取并删除
  189. * @param string $name session名称
  190. * @param string|null $prefix 作用域(前缀)
  191. * @return mixed
  192. */
  193. public static function pull($name, $prefix = null)
  194. {
  195. $result = self::get($name, $prefix);
  196. if ($result) {
  197. self::delete($name, $prefix);
  198. return $result;
  199. } else {
  200. return;
  201. }
  202. }
  203. /**
  204. * session设置 下一次请求有效
  205. * @param string $name session名称
  206. * @param mixed $value session值
  207. * @param string|null $prefix 作用域(前缀)
  208. * @return void
  209. */
  210. public static function flash($name, $value)
  211. {
  212. self::set($name, $value);
  213. if (!self::has('__flash__.__time__')) {
  214. self::set('__flash__.__time__', $_SERVER['REQUEST_TIME_FLOAT']);
  215. }
  216. self::push('__flash__', $name);
  217. }
  218. /**
  219. * 清空当前请求的session数据
  220. * @return void
  221. */
  222. public static function flush()
  223. {
  224. if (self::$init) {
  225. $item = self::get('__flash__');
  226. if (!empty($item)) {
  227. $time = $item['__time__'];
  228. if ($_SERVER['REQUEST_TIME_FLOAT'] > $time) {
  229. unset($item['__time__']);
  230. self::delete($item);
  231. self::set('__flash__', []);
  232. }
  233. }
  234. }
  235. }
  236. /**
  237. * 删除session数据
  238. * @param string|array $name session名称
  239. * @param string|null $prefix 作用域(前缀)
  240. * @return void
  241. */
  242. public static function delete($name, $prefix = null)
  243. {
  244. empty(self::$init) && self::boot();
  245. $prefix = !is_null($prefix) ? $prefix : self::$prefix;
  246. if (is_array($name)) {
  247. foreach ($name as $key) {
  248. self::delete($key, $prefix);
  249. }
  250. } elseif (strpos($name, '.')) {
  251. list($name1, $name2) = explode('.', $name);
  252. if ($prefix) {
  253. unset($_SESSION[$prefix][$name1][$name2]);
  254. } else {
  255. unset($_SESSION[$name1][$name2]);
  256. }
  257. } else {
  258. if ($prefix) {
  259. unset($_SESSION[$prefix][$name]);
  260. } else {
  261. unset($_SESSION[$name]);
  262. }
  263. }
  264. }
  265. /**
  266. * 清空session数据
  267. * @param string|null $prefix 作用域(前缀)
  268. * @return void
  269. */
  270. public static function clear($prefix = null)
  271. {
  272. empty(self::$init) && self::boot();
  273. $prefix = !is_null($prefix) ? $prefix : self::$prefix;
  274. if ($prefix) {
  275. unset($_SESSION[$prefix]);
  276. } else {
  277. $_SESSION = [];
  278. }
  279. }
  280. /**
  281. * 判断session数据
  282. * @param string $name session名称
  283. * @param string|null $prefix
  284. * @return bool
  285. */
  286. public static function has($name, $prefix = null)
  287. {
  288. empty(self::$init) && self::boot();
  289. $prefix = !is_null($prefix) ? $prefix : self::$prefix;
  290. if (strpos($name, '.')) {
  291. // 支持数组
  292. list($name1, $name2) = explode('.', $name);
  293. return $prefix ? isset($_SESSION[$prefix][$name1][$name2]) : isset($_SESSION[$name1][$name2]);
  294. } else {
  295. return $prefix ? isset($_SESSION[$prefix][$name]) : isset($_SESSION[$name]);
  296. }
  297. }
  298. /**
  299. * 添加数据到一个session数组
  300. * @param string $key
  301. * @param mixed $value
  302. * @return void
  303. */
  304. public static function push($key, $value)
  305. {
  306. $array = self::get($key);
  307. if (is_null($array)) {
  308. $array = [];
  309. }
  310. $array[] = $value;
  311. self::set($key, $array);
  312. }
  313. /**
  314. * 启动session
  315. * @return void
  316. */
  317. public static function start()
  318. {
  319. session_start();
  320. self::$init = true;
  321. }
  322. /**
  323. * 销毁session
  324. * @return void
  325. */
  326. public static function destroy()
  327. {
  328. if (!empty($_SESSION)) {
  329. $_SESSION = [];
  330. }
  331. session_unset();
  332. session_destroy();
  333. self::$init = null;
  334. }
  335. /**
  336. * 重新生成session_id
  337. * @param bool $delete 是否删除关联会话文件
  338. * @return void
  339. */
  340. public static function regenerate($delete = false)
  341. {
  342. session_regenerate_id($delete);
  343. }
  344. /**
  345. * 暂停session
  346. * @return void
  347. */
  348. public static function pause()
  349. {
  350. // 暂停session
  351. session_write_close();
  352. self::$init = false;
  353. }
  354. }