Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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