/thinkphp/library/think/Cookie.php

https://github.com/yuan1994/tpAdmin · PHP · 211 lines · 126 code · 13 blank · 72 comment · 26 complexity · 6358b42e71f847a7b9de64fea64f7f64 MD5 · raw file

  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2017 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. class Cookie
  13. {
  14. protected static $config = [
  15. // cookie 名称前缀
  16. 'prefix' => '',
  17. // cookie 保存时间
  18. 'expire' => 0,
  19. // cookie 保存路径
  20. 'path' => '/',
  21. // cookie 有效域名
  22. 'domain' => '',
  23. // cookie 启用安全传输
  24. 'secure' => false,
  25. // httponly设置
  26. 'httponly' => '',
  27. // 是否使用 setcookie
  28. 'setcookie' => true,
  29. ];
  30. protected static $init;
  31. /**
  32. * Cookie初始化
  33. * @param array $config
  34. * @return void
  35. */
  36. public static function init(array $config = [])
  37. {
  38. if (empty($config)) {
  39. $config = Config::get('cookie');
  40. }
  41. self::$config = array_merge(self::$config, array_change_key_case($config));
  42. if (!empty(self::$config['httponly'])) {
  43. ini_set('session.cookie_httponly', 1);
  44. }
  45. self::$init = true;
  46. }
  47. /**
  48. * 设置或者获取cookie作用域(前缀)
  49. * @param string $prefix
  50. * @return string|void
  51. */
  52. public static function prefix($prefix = '')
  53. {
  54. if (empty($prefix)) {
  55. return self::$config['prefix'];
  56. }
  57. self::$config['prefix'] = $prefix;
  58. }
  59. /**
  60. * Cookie 设置、获取、删除
  61. *
  62. * @param string $name cookie名称
  63. * @param mixed $value cookie值
  64. * @param mixed $option 可选参数 可能会是 null|integer|string
  65. *
  66. * @return mixed
  67. * @internal param mixed $options cookie参数
  68. */
  69. public static function set($name, $value = '', $option = null)
  70. {
  71. !isset(self::$init) && self::init();
  72. // 参数设置(会覆盖黙认设置)
  73. if (!is_null($option)) {
  74. if (is_numeric($option)) {
  75. $option = ['expire' => $option];
  76. } elseif (is_string($option)) {
  77. parse_str($option, $option);
  78. }
  79. $config = array_merge(self::$config, array_change_key_case($option));
  80. } else {
  81. $config = self::$config;
  82. }
  83. $name = $config['prefix'] . $name;
  84. // 设置cookie
  85. if (is_array($value)) {
  86. array_walk_recursive($value, 'self::jsonFormatProtect', 'encode');
  87. $value = 'think:' . json_encode($value);
  88. }
  89. $expire = !empty($config['expire']) ? $_SERVER['REQUEST_TIME'] + intval($config['expire']) : 0;
  90. if ($config['setcookie']) {
  91. setcookie($name, $value, $expire, $config['path'], $config['domain'], $config['secure'], $config['httponly']);
  92. }
  93. $_COOKIE[$name] = $value;
  94. }
  95. /**
  96. * 永久保存Cookie数据
  97. * @param string $name cookie名称
  98. * @param mixed $value cookie值
  99. * @param mixed $option 可选参数 可能会是 null|integer|string
  100. * @return void
  101. */
  102. public static function forever($name, $value = '', $option = null)
  103. {
  104. if (is_null($option) || is_numeric($option)) {
  105. $option = [];
  106. }
  107. $option['expire'] = 315360000;
  108. self::set($name, $value, $option);
  109. }
  110. /**
  111. * 判断Cookie数据
  112. * @param string $name cookie名称
  113. * @param string|null $prefix cookie前缀
  114. * @return bool
  115. */
  116. public static function has($name, $prefix = null)
  117. {
  118. !isset(self::$init) && self::init();
  119. $prefix = !is_null($prefix) ? $prefix : self::$config['prefix'];
  120. $name = $prefix . $name;
  121. return isset($_COOKIE[$name]);
  122. }
  123. /**
  124. * Cookie获取
  125. * @param string $name cookie名称
  126. * @param string|null $prefix cookie前缀
  127. * @return mixed
  128. */
  129. public static function get($name, $prefix = null)
  130. {
  131. !isset(self::$init) && self::init();
  132. $prefix = !is_null($prefix) ? $prefix : self::$config['prefix'];
  133. $name = $prefix . $name;
  134. if (isset($_COOKIE[$name])) {
  135. $value = $_COOKIE[$name];
  136. if (0 === strpos($value, 'think:')) {
  137. $value = substr($value, 6);
  138. $value = json_decode($value, true);
  139. array_walk_recursive($value, 'self::jsonFormatProtect', 'decode');
  140. }
  141. return $value;
  142. } else {
  143. return;
  144. }
  145. }
  146. /**
  147. * Cookie删除
  148. * @param string $name cookie名称
  149. * @param string|null $prefix cookie前缀
  150. * @return mixed
  151. */
  152. public static function delete($name, $prefix = null)
  153. {
  154. !isset(self::$init) && self::init();
  155. $config = self::$config;
  156. $prefix = !is_null($prefix) ? $prefix : $config['prefix'];
  157. $name = $prefix . $name;
  158. if ($config['setcookie']) {
  159. setcookie($name, '', $_SERVER['REQUEST_TIME'] - 3600, $config['path'], $config['domain'], $config['secure'], $config['httponly']);
  160. }
  161. // 删除指定cookie
  162. unset($_COOKIE[$name]);
  163. }
  164. /**
  165. * Cookie清空
  166. * @param string|null $prefix cookie前缀
  167. * @return mixed
  168. */
  169. public static function clear($prefix = null)
  170. {
  171. // 清除指定前缀的所有cookie
  172. if (empty($_COOKIE)) {
  173. return;
  174. }
  175. !isset(self::$init) && self::init();
  176. // 要删除的cookie前缀,不指定则删除config设置的指定前缀
  177. $config = self::$config;
  178. $prefix = !is_null($prefix) ? $prefix : $config['prefix'];
  179. if ($prefix) {
  180. // 如果前缀为空字符串将不作处理直接返回
  181. foreach ($_COOKIE as $key => $val) {
  182. if (0 === strpos($key, $prefix)) {
  183. if ($config['setcookie']) {
  184. setcookie($key, '', $_SERVER['REQUEST_TIME'] - 3600, $config['path'], $config['domain'], $config['secure'], $config['httponly']);
  185. }
  186. unset($_COOKIE[$key]);
  187. }
  188. }
  189. }
  190. return;
  191. }
  192. private static function jsonFormatProtect(&$val, $key, $type = 'encode')
  193. {
  194. if (!empty($val) && true !== $val) {
  195. $val = 'decode' == $type ? urldecode($val) : urlencode($val);
  196. }
  197. }
  198. }