Cookie.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. class Cookie
  13. {
  14. /**
  15. * @var array cookie 设置参数
  16. */
  17. protected static $config = [
  18. 'prefix' => '', // cookie 名称前缀
  19. 'expire' => 0, // cookie 保存时间
  20. 'path' => '/', // cookie 保存路径
  21. 'domain' => '', // cookie 有效域名
  22. 'secure' => false, // cookie 启用安全传输
  23. 'httponly' => false, // httponly 设置
  24. 'setcookie' => true, // 是否使用 setcookie
  25. ];
  26. /**
  27. * @var bool 是否完成初始化了
  28. */
  29. protected static $init;
  30. /**
  31. * Cookie初始化
  32. * @access public
  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. * @access public
  50. * @param string $prefix 前缀
  51. * @return string|
  52. */
  53. public static function prefix($prefix = '')
  54. {
  55. if (empty($prefix)) {
  56. return self::$config['prefix'];
  57. }
  58. return self::$config['prefix'] = $prefix;
  59. }
  60. /**
  61. * Cookie 设置、获取、删除
  62. * @access public
  63. * @param string $name cookie 名称
  64. * @param mixed $value cookie 值
  65. * @param mixed $option 可选参数 可能会是 null|integer|string
  66. * @return void
  67. */
  68. public static function set($name, $value = '', $option = null)
  69. {
  70. !isset(self::$init) && self::init();
  71. // 参数设置(会覆盖黙认设置)
  72. if (!is_null($option)) {
  73. if (is_numeric($option)) {
  74. $option = ['expire' => $option];
  75. } elseif (is_string($option)) {
  76. parse_str($option, $option);
  77. }
  78. $config = array_merge(self::$config, array_change_key_case($option));
  79. } else {
  80. $config = self::$config;
  81. }
  82. $name = $config['prefix'] . $name;
  83. // 设置 cookie
  84. if (is_array($value)) {
  85. array_walk_recursive($value, 'self::jsonFormatProtect', 'encode');
  86. $value = 'think:' . json_encode($value);
  87. }
  88. $expire = !empty($config['expire']) ?
  89. $_SERVER['REQUEST_TIME'] + intval($config['expire']) :
  90. 0;
  91. if ($config['setcookie']) {
  92. setcookie(
  93. $name, $value, $expire, $config['path'], $config['domain'],
  94. $config['secure'], $config['httponly']
  95. );
  96. }
  97. $_COOKIE[$name] = $value;
  98. }
  99. /**
  100. * 永久保存 Cookie 数据
  101. * @access public
  102. * @param string $name cookie 名称
  103. * @param mixed $value cookie 值
  104. * @param mixed $option 可选参数 可能会是 null|integer|string
  105. * @return void
  106. */
  107. public static function forever($name, $value = '', $option = null)
  108. {
  109. if (is_null($option) || is_numeric($option)) {
  110. $option = [];
  111. }
  112. $option['expire'] = 315360000;
  113. self::set($name, $value, $option);
  114. }
  115. /**
  116. * 判断是否有 Cookie 数据
  117. * @access public
  118. * @param string $name cookie 名称
  119. * @param string|null $prefix cookie 前缀
  120. * @return bool
  121. */
  122. public static function has($name, $prefix = null)
  123. {
  124. !isset(self::$init) && self::init();
  125. $prefix = !is_null($prefix) ? $prefix : self::$config['prefix'];
  126. return isset($_COOKIE[$prefix . $name]);
  127. }
  128. /**
  129. * 获取 Cookie 的值
  130. * @access public
  131. * @param string $name cookie 名称
  132. * @param string|null $prefix cookie 前缀
  133. * @return mixed
  134. */
  135. public static function get($name = '', $prefix = null)
  136. {
  137. !isset(self::$init) && self::init();
  138. $prefix = !is_null($prefix) ? $prefix : self::$config['prefix'];
  139. $key = $prefix . $name;
  140. if ('' == $name) {
  141. // 获取全部
  142. if ($prefix) {
  143. $value = [];
  144. foreach ($_COOKIE as $k => $val) {
  145. if (0 === strpos($k, $prefix)) {
  146. $value[$k] = $val;
  147. }
  148. }
  149. } else {
  150. $value = $_COOKIE;
  151. }
  152. } elseif (isset($_COOKIE[$key])) {
  153. $value = $_COOKIE[$key];
  154. if (0 === strpos($value, 'think:')) {
  155. $value = json_decode(substr($value, 6), true);
  156. array_walk_recursive($value, 'self::jsonFormatProtect', 'decode');
  157. }
  158. } else {
  159. $value = null;
  160. }
  161. return $value;
  162. }
  163. /**
  164. * 删除 Cookie
  165. * @access public
  166. * @param string $name cookie 名称
  167. * @param string|null $prefix cookie 前缀
  168. * @return void
  169. */
  170. public static function delete($name, $prefix = null)
  171. {
  172. !isset(self::$init) && self::init();
  173. $config = self::$config;
  174. $prefix = !is_null($prefix) ? $prefix : $config['prefix'];
  175. $name = $prefix . $name;
  176. if ($config['setcookie']) {
  177. setcookie(
  178. $name, '', $_SERVER['REQUEST_TIME'] - 3600, $config['path'],
  179. $config['domain'], $config['secure'], $config['httponly']
  180. );
  181. }
  182. // 删除指定 cookie
  183. unset($_COOKIE[$name]);
  184. }
  185. /**
  186. * 清除指定前缀的所有 cookie
  187. * @access public
  188. * @param string|null $prefix cookie 前缀
  189. * @return void
  190. */
  191. public static function clear($prefix = null)
  192. {
  193. if (empty($_COOKIE)) {
  194. return;
  195. }
  196. !isset(self::$init) && self::init();
  197. // 要删除的 cookie 前缀,不指定则删除 config 设置的指定前缀
  198. $config = self::$config;
  199. $prefix = !is_null($prefix) ? $prefix : $config['prefix'];
  200. if ($prefix) {
  201. foreach ($_COOKIE as $key => $val) {
  202. if (0 === strpos($key, $prefix)) {
  203. if ($config['setcookie']) {
  204. setcookie(
  205. $key, '', $_SERVER['REQUEST_TIME'] - 3600, $config['path'],
  206. $config['domain'], $config['secure'], $config['httponly']
  207. );
  208. }
  209. unset($_COOKIE[$key]);
  210. }
  211. }
  212. }
  213. }
  214. /**
  215. * json 转换时的格式保护
  216. * @access protected
  217. * @param mixed $val 要转换的值
  218. * @param string $key 键名
  219. * @param string $type 转换类别
  220. * @return void
  221. */
  222. protected static function jsonFormatProtect(&$val, $key, $type = 'encode')
  223. {
  224. if (!empty($val) && true !== $val) {
  225. $val = 'decode' == $type ? urldecode($val) : urlencode($val);
  226. }
  227. }
  228. }