Session.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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. /**
  16. * 配置参数
  17. * @var array
  18. */
  19. protected $config = [];
  20. /**
  21. * 前缀
  22. * @var string
  23. */
  24. protected $prefix = '';
  25. /**
  26. * 是否初始化
  27. * @var bool
  28. */
  29. protected $init = null;
  30. /**
  31. * 锁驱动
  32. * @var object
  33. */
  34. protected $lockDriver = null;
  35. /**
  36. * 锁key
  37. * @var string
  38. */
  39. protected $sessKey = 'PHPSESSID';
  40. /**
  41. * 锁超时时间
  42. * @var integer
  43. */
  44. protected $lockTimeout = 3;
  45. /**
  46. * 是否启用锁机制
  47. * @var bool
  48. */
  49. protected $lock = false;
  50. public function __construct(array $config = [])
  51. {
  52. $this->config = $config;
  53. }
  54. /**
  55. * 设置或者获取session作用域(前缀)
  56. * @access public
  57. * @param string $prefix
  58. * @return string|void
  59. */
  60. public function prefix($prefix = '')
  61. {
  62. empty($this->init) && $this->boot();
  63. if (empty($prefix) && null !== $prefix) {
  64. return $this->prefix;
  65. } else {
  66. $this->prefix = $prefix;
  67. }
  68. }
  69. public static function __make(Config $config)
  70. {
  71. return new static($config->pull('session'));
  72. }
  73. /**
  74. * 配置
  75. * @access public
  76. * @param array $config
  77. * @return void
  78. */
  79. public function setConfig(array $config = [])
  80. {
  81. $this->config = array_merge($this->config, array_change_key_case($config));
  82. if (isset($config['prefix'])) {
  83. $this->prefix = $config['prefix'];
  84. }
  85. if (isset($config['use_lock'])) {
  86. $this->lock = $config['use_lock'];
  87. }
  88. }
  89. /**
  90. * 设置已经初始化
  91. * @access public
  92. * @return void
  93. */
  94. public function inited()
  95. {
  96. $this->init = true;
  97. }
  98. /**
  99. * session初始化
  100. * @access public
  101. * @param array $config
  102. * @return void
  103. * @throws \think\Exception
  104. */
  105. public function init(array $config = [])
  106. {
  107. $config = $config ?: $this->config;
  108. $isDoStart = false;
  109. if (isset($config['use_trans_sid'])) {
  110. ini_set('session.use_trans_sid', $config['use_trans_sid'] ? 1 : 0);
  111. }
  112. // 启动session
  113. if (!empty($config['auto_start']) && PHP_SESSION_ACTIVE != session_status()) {
  114. ini_set('session.auto_start', 0);
  115. $isDoStart = true;
  116. }
  117. if (isset($config['prefix'])) {
  118. $this->prefix = $config['prefix'];
  119. }
  120. if (isset($config['use_lock'])) {
  121. $this->lock = $config['use_lock'];
  122. }
  123. if (isset($config['var_session_id']) && isset($_REQUEST[$config['var_session_id']])) {
  124. session_id($_REQUEST[$config['var_session_id']]);
  125. } elseif (isset($config['id']) && !empty($config['id'])) {
  126. session_id($config['id']);
  127. }
  128. if (isset($config['name'])) {
  129. session_name($config['name']);
  130. }
  131. if (isset($config['path'])) {
  132. session_save_path($config['path']);
  133. }
  134. if (isset($config['domain'])) {
  135. ini_set('session.cookie_domain', $config['domain']);
  136. }
  137. if (isset($config['expire'])) {
  138. ini_set('session.gc_maxlifetime', $config['expire']);
  139. ini_set('session.cookie_lifetime', $config['expire']);
  140. }
  141. if (isset($config['secure'])) {
  142. ini_set('session.cookie_secure', $config['secure']);
  143. }
  144. if (isset($config['httponly'])) {
  145. ini_set('session.cookie_httponly', $config['httponly']);
  146. }
  147. if (isset($config['use_cookies'])) {
  148. ini_set('session.use_cookies', $config['use_cookies'] ? 1 : 0);
  149. }
  150. if (isset($config['cache_limiter'])) {
  151. session_cache_limiter($config['cache_limiter']);
  152. }
  153. if (isset($config['cache_expire'])) {
  154. session_cache_expire($config['cache_expire']);
  155. }
  156. if (!empty($config['type'])) {
  157. // 读取session驱动
  158. $class = false !== strpos($config['type'], '\\') ? $config['type'] : '\\think\\session\\driver\\' . ucwords($config['type']);
  159. // 检查驱动类
  160. if (!class_exists($class) || !session_set_save_handler(new $class($config))) {
  161. throw new ClassNotFoundException('error session handler:' . $class, $class);
  162. }
  163. }
  164. if ($isDoStart) {
  165. $this->start();
  166. } else {
  167. $this->init = false;
  168. }
  169. return $this;
  170. }
  171. /**
  172. * session自动启动或者初始化
  173. * @access public
  174. * @return void
  175. */
  176. public function boot()
  177. {
  178. if (is_null($this->init)) {
  179. $this->init();
  180. }
  181. if (false === $this->init) {
  182. if (PHP_SESSION_ACTIVE != session_status()) {
  183. $this->start();
  184. }
  185. $this->init = true;
  186. }
  187. }
  188. /**
  189. * session设置
  190. * @access public
  191. * @param string $name session名称
  192. * @param mixed $value session值
  193. * @param string|null $prefix 作用域(前缀)
  194. * @return void
  195. */
  196. public function set($name, $value, $prefix = null)
  197. {
  198. $this->lock();
  199. empty($this->init) && $this->boot();
  200. $prefix = !is_null($prefix) ? $prefix : $this->prefix;
  201. if (strpos($name, '.')) {
  202. // 二维数组赋值
  203. list($name1, $name2) = explode('.', $name);
  204. if ($prefix) {
  205. $_SESSION[$prefix][$name1][$name2] = $value;
  206. } else {
  207. $_SESSION[$name1][$name2] = $value;
  208. }
  209. } elseif ($prefix) {
  210. $_SESSION[$prefix][$name] = $value;
  211. } else {
  212. $_SESSION[$name] = $value;
  213. }
  214. $this->unlock();
  215. }
  216. /**
  217. * session获取
  218. * @access public
  219. * @param string $name session名称
  220. * @param string|null $prefix 作用域(前缀)
  221. * @return mixed
  222. */
  223. public function get($name = '', $prefix = null)
  224. {
  225. $this->lock();
  226. empty($this->init) && $this->boot();
  227. $prefix = !is_null($prefix) ? $prefix : $this->prefix;
  228. $value = $prefix ? (!empty($_SESSION[$prefix]) ? $_SESSION[$prefix] : []) : $_SESSION;
  229. if ('' != $name) {
  230. $name = explode('.', $name);
  231. foreach ($name as $val) {
  232. if (isset($value[$val])) {
  233. $value = $value[$val];
  234. } else {
  235. $value = null;
  236. break;
  237. }
  238. }
  239. }
  240. $this->unlock();
  241. return $value;
  242. }
  243. /**
  244. * session 读写锁驱动实例化
  245. */
  246. protected function initDriver()
  247. {
  248. $config = $this->config;
  249. if (!empty($config['type']) && isset($config['use_lock']) && $config['use_lock']) {
  250. // 读取session驱动
  251. $class = false !== strpos($config['type'], '\\') ? $config['type'] : '\\think\\session\\driver\\' . ucwords($config['type']);
  252. // 检查驱动类及类中是否存在 lock 和 unlock 函数
  253. if (class_exists($class) && method_exists($class, 'lock') && method_exists($class, 'unlock')) {
  254. $this->lockDriver = new $class($config);
  255. }
  256. }
  257. // 通过cookie获得session_id
  258. if (isset($config['name']) && $config['name']) {
  259. $this->sessKey = $config['name'];
  260. }
  261. if (isset($config['lock_timeout']) && $config['lock_timeout'] > 0) {
  262. $this->lockTimeout = $config['lock_timeout'];
  263. }
  264. }
  265. /**
  266. * session 读写加锁
  267. * @access protected
  268. * @return void
  269. */
  270. protected function lock()
  271. {
  272. if (empty($this->lock)) {
  273. return;
  274. }
  275. $this->initDriver();
  276. if (null !== $this->lockDriver && method_exists($this->lockDriver, 'lock')) {
  277. $t = time();
  278. // 使用 session_id 作为互斥条件,即只对同一 session_id 的会话互斥。第一次请求没有 session_id
  279. $sessID = isset($_COOKIE[$this->sessKey]) ? $_COOKIE[$this->sessKey] : '';
  280. do {
  281. if (time() - $t > $this->lockTimeout) {
  282. $this->unlock();
  283. }
  284. } while (!$this->lockDriver->lock($sessID, $this->lockTimeout));
  285. }
  286. }
  287. /**
  288. * session 读写解锁
  289. * @access protected
  290. * @return void
  291. */
  292. protected function unlock()
  293. {
  294. if (empty($this->lock)) {
  295. return;
  296. }
  297. $this->pause();
  298. if ($this->lockDriver && method_exists($this->lockDriver, 'unlock')) {
  299. $sessID = isset($_COOKIE[$this->sessKey]) ? $_COOKIE[$this->sessKey] : '';
  300. $this->lockDriver->unlock($sessID);
  301. }
  302. }
  303. /**
  304. * session获取并删除
  305. * @access public
  306. * @param string $name session名称
  307. * @param string|null $prefix 作用域(前缀)
  308. * @return mixed
  309. */
  310. public function pull($name, $prefix = null)
  311. {
  312. $result = $this->get($name, $prefix);
  313. if ($result) {
  314. $this->delete($name, $prefix);
  315. return $result;
  316. } else {
  317. return;
  318. }
  319. }
  320. /**
  321. * session设置 下一次请求有效
  322. * @access public
  323. * @param string $name session名称
  324. * @param mixed $value session值
  325. * @param string|null $prefix 作用域(前缀)
  326. * @return void
  327. */
  328. public function flash($name, $value)
  329. {
  330. $this->set($name, $value);
  331. if (!$this->has('__flash__.__time__')) {
  332. $this->set('__flash__.__time__', $_SERVER['REQUEST_TIME_FLOAT']);
  333. }
  334. $this->push('__flash__', $name);
  335. }
  336. /**
  337. * 清空当前请求的session数据
  338. * @access public
  339. * @return void
  340. */
  341. public function flush()
  342. {
  343. if (!$this->init) {
  344. return;
  345. }
  346. $item = $this->get('__flash__');
  347. if (!empty($item)) {
  348. $time = $item['__time__'];
  349. if ($_SERVER['REQUEST_TIME_FLOAT'] > $time) {
  350. unset($item['__time__']);
  351. $this->delete($item);
  352. $this->set('__flash__', []);
  353. }
  354. }
  355. }
  356. /**
  357. * 删除session数据
  358. * @access public
  359. * @param string|array $name session名称
  360. * @param string|null $prefix 作用域(前缀)
  361. * @return void
  362. */
  363. public function delete($name, $prefix = null)
  364. {
  365. empty($this->init) && $this->boot();
  366. $prefix = !is_null($prefix) ? $prefix : $this->prefix;
  367. if (is_array($name)) {
  368. foreach ($name as $key) {
  369. $this->delete($key, $prefix);
  370. }
  371. } elseif (strpos($name, '.')) {
  372. list($name1, $name2) = explode('.', $name);
  373. if ($prefix) {
  374. unset($_SESSION[$prefix][$name1][$name2]);
  375. } else {
  376. unset($_SESSION[$name1][$name2]);
  377. }
  378. } else {
  379. if ($prefix) {
  380. unset($_SESSION[$prefix][$name]);
  381. } else {
  382. unset($_SESSION[$name]);
  383. }
  384. }
  385. }
  386. /**
  387. * 清空session数据
  388. * @access public
  389. * @param string|null $prefix 作用域(前缀)
  390. * @return void
  391. */
  392. public function clear($prefix = null)
  393. {
  394. empty($this->init) && $this->boot();
  395. $prefix = !is_null($prefix) ? $prefix : $this->prefix;
  396. if ($prefix) {
  397. unset($_SESSION[$prefix]);
  398. } else {
  399. $_SESSION = [];
  400. }
  401. }
  402. /**
  403. * 判断session数据
  404. * @access public
  405. * @param string $name session名称
  406. * @param string|null $prefix
  407. * @return bool
  408. */
  409. public function has($name, $prefix = null)
  410. {
  411. empty($this->init) && $this->boot();
  412. $prefix = !is_null($prefix) ? $prefix : $this->prefix;
  413. $value = $prefix ? (!empty($_SESSION[$prefix]) ? $_SESSION[$prefix] : []) : $_SESSION;
  414. $name = explode('.', $name);
  415. foreach ($name as $val) {
  416. if (!isset($value[$val])) {
  417. return false;
  418. } else {
  419. $value = $value[$val];
  420. }
  421. }
  422. return true;
  423. }
  424. /**
  425. * 添加数据到一个session数组
  426. * @access public
  427. * @param string $key
  428. * @param mixed $value
  429. * @return void
  430. */
  431. public function push($key, $value)
  432. {
  433. $array = $this->get($key);
  434. if (is_null($array)) {
  435. $array = [];
  436. }
  437. $array[] = $value;
  438. $this->set($key, $array);
  439. }
  440. /**
  441. * 启动session
  442. * @access public
  443. * @return void
  444. */
  445. public function start()
  446. {
  447. session_start();
  448. $this->init = true;
  449. }
  450. /**
  451. * 销毁session
  452. * @access public
  453. * @return void
  454. */
  455. public function destroy()
  456. {
  457. if (!empty($_SESSION)) {
  458. $_SESSION = [];
  459. }
  460. session_unset();
  461. session_destroy();
  462. $this->init = null;
  463. $this->lockDriver = null;
  464. }
  465. /**
  466. * 重新生成session_id
  467. * @access public
  468. * @param bool $delete 是否删除关联会话文件
  469. * @return void
  470. */
  471. public function regenerate($delete = false)
  472. {
  473. session_regenerate_id($delete);
  474. }
  475. /**
  476. * 暂停session
  477. * @access public
  478. * @return void
  479. */
  480. public function pause()
  481. {
  482. // 暂停session
  483. session_write_close();
  484. $this->init = false;
  485. }
  486. }