Cookie.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. $key = $prefix . $name;
  134. if ('' == $name) {
  135. // 获取全部
  136. if ($prefix) {
  137. $value = [];
  138. foreach ($_COOKIE as $k => $val) {
  139. if (0 === strpos($k, $prefix)) {
  140. $value[$k] = $val;
  141. }
  142. }
  143. } else {
  144. $value = $_COOKIE;
  145. }
  146. } elseif (isset($_COOKIE[$key])) {
  147. $value = $_COOKIE[$key];
  148. if (0 === strpos($value, 'think:')) {
  149. $value = substr($value, 6);
  150. $value = json_decode($value, true);
  151. array_walk_recursive($value, 'self::jsonFormatProtect', 'decode');
  152. }
  153. } else {
  154. $value = null;
  155. }
  156. return $value;
  157. }
  158. /**
  159. * Cookie删除
  160. * @param string $name cookie名称
  161. * @param string|null $prefix cookie前缀
  162. * @return mixed
  163. */
  164. public static function delete($name, $prefix = null)
  165. {
  166. !isset(self::$init) && self::init();
  167. $config = self::$config;
  168. $prefix = !is_null($prefix) ? $prefix : $config['prefix'];
  169. $name = $prefix . $name;
  170. if ($config['setcookie']) {
  171. setcookie($name, '', $_SERVER['REQUEST_TIME'] - 3600, $config['path'], $config['domain'], $config['secure'], $config['httponly']);
  172. }
  173. // 删除指定cookie
  174. unset($_COOKIE[$name]);
  175. }
  176. /**
  177. * Cookie清空
  178. * @param string|null $prefix cookie前缀
  179. * @return mixed
  180. */
  181. public static function clear($prefix = null)
  182. {
  183. // 清除指定前缀的所有cookie
  184. if (empty($_COOKIE)) {
  185. return;
  186. }
  187. !isset(self::$init) && self::init();
  188. // 要删除的cookie前缀,不指定则删除config设置的指定前缀
  189. $config = self::$config;
  190. $prefix = !is_null($prefix) ? $prefix : $config['prefix'];
  191. if ($prefix) {
  192. // 如果前缀为空字符串将不作处理直接返回
  193. foreach ($_COOKIE as $key => $val) {
  194. if (0 === strpos($key, $prefix)) {
  195. if ($config['setcookie']) {
  196. setcookie($key, '', $_SERVER['REQUEST_TIME'] - 3600, $config['path'], $config['domain'], $config['secure'], $config['httponly']);
  197. }
  198. unset($_COOKIE[$key]);
  199. }
  200. }
  201. }
  202. return;
  203. }
  204. private static function jsonFormatProtect(&$val, $key, $type = 'encode')
  205. {
  206. if (!empty($val) && true !== $val) {
  207. $val = 'decode' == $type ? urldecode($val) : urlencode($val);
  208. }
  209. }
  210. }