Cookie.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2019 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. declare (strict_types = 1);
  12. namespace think;
  13. use DateTimeInterface;
  14. /**
  15. * Cookie管理类
  16. * @package think
  17. */
  18. class Cookie
  19. {
  20. /**
  21. * 配置参数
  22. * @var array
  23. */
  24. protected $config = [
  25. // cookie 保存时间
  26. 'expire' => 0,
  27. // cookie 保存路径
  28. 'path' => '/',
  29. // cookie 有效域名
  30. 'domain' => '',
  31. // cookie 启用安全传输
  32. 'secure' => false,
  33. // httponly设置
  34. 'httponly' => false,
  35. ];
  36. /**
  37. * Cookie写入数据
  38. * @var array
  39. */
  40. protected $cookie = [];
  41. /**
  42. * 当前Request对象
  43. * @var Request
  44. */
  45. protected $request;
  46. /**
  47. * 构造方法
  48. * @access public
  49. */
  50. public function __construct(Request $request, array $config = [])
  51. {
  52. $this->request = $request;
  53. $this->config = array_merge($this->config, array_change_key_case($config));
  54. }
  55. public static function __make(Request $request, Config $config)
  56. {
  57. return new static($request, $config->get('cookie'));
  58. }
  59. /**
  60. * 获取cookie
  61. * @access public
  62. * @param mixed $name 数据名称
  63. * @param string $default 默认值
  64. * @return mixed
  65. */
  66. public function get(string $name = '', $default = null)
  67. {
  68. return $this->request->cookie($name, $default);
  69. }
  70. /**
  71. * 是否存在Cookie参数
  72. * @access public
  73. * @param string $name 变量名
  74. * @return bool
  75. */
  76. public function has(string $name): bool
  77. {
  78. return $this->request->has($name, 'cookie');
  79. }
  80. /**
  81. * Cookie 设置
  82. *
  83. * @access public
  84. * @param string $name cookie名称
  85. * @param string $value cookie值
  86. * @param mixed $option 可选参数
  87. * @return void
  88. */
  89. public function set(string $name, string $value, $option = null): void
  90. {
  91. // 参数设置(会覆盖黙认设置)
  92. if (!is_null($option)) {
  93. if (is_numeric($option) || $option instanceof DateTimeInterface) {
  94. $option = ['expire' => $option];
  95. }
  96. $config = array_merge($this->config, array_change_key_case($option));
  97. } else {
  98. $config = $this->config;
  99. }
  100. if ($config['expire'] instanceof DateTimeInterface) {
  101. $expire = $config['expire']->getTimestamp();
  102. } else {
  103. $expire = !empty($config['expire']) ? time() + intval($config['expire']) : 0;
  104. }
  105. $this->setCookie($name, $value, $expire, $config);
  106. }
  107. /**
  108. * Cookie 保存
  109. *
  110. * @access public
  111. * @param string $name cookie名称
  112. * @param string $value cookie值
  113. * @param int $expire 有效期
  114. * @param array $option 可选参数
  115. * @return void
  116. */
  117. protected function setCookie(string $name, string $value, int $expire, array $option = []): void
  118. {
  119. $this->cookie[$name] = [$value, $expire, $option];
  120. }
  121. /**
  122. * 永久保存Cookie数据
  123. * @access public
  124. * @param string $name cookie名称
  125. * @param string $value cookie值
  126. * @param mixed $option 可选参数 可能会是 null|integer|string
  127. * @return void
  128. */
  129. public function forever(string $name, string $value = '', $option = null): void
  130. {
  131. if (is_null($option) || is_numeric($option)) {
  132. $option = [];
  133. }
  134. $option['expire'] = 315360000;
  135. $this->set($name, $value, $option);
  136. }
  137. /**
  138. * Cookie删除
  139. * @access public
  140. * @param string $name cookie名称
  141. * @return void
  142. */
  143. public function delete(string $name): void
  144. {
  145. $this->setCookie($name, '', time() - 3600, $this->config);
  146. }
  147. /**
  148. * 获取cookie保存数据
  149. * @access public
  150. * @return array
  151. */
  152. public function getCookie(): array
  153. {
  154. return $this->cookie;
  155. }
  156. /**
  157. * 保存Cookie
  158. * @access public
  159. * @return void
  160. */
  161. public function save(): void
  162. {
  163. foreach ($this->cookie as $name => $val) {
  164. list($value, $expire, $option) = $val;
  165. $this->saveCookie($name, $value, $expire, $option['path'], $option['domain'], $option['secure'] ? true : false, $option['httponly'] ? true : false);
  166. }
  167. }
  168. /**
  169. * 保存Cookie
  170. * @access public
  171. * @param string $name cookie名称
  172. * @param string $value cookie值
  173. * @param int $expire cookie过期时间
  174. * @param string $path 有效的服务器路径
  175. * @param string $domain 有效域名/子域名
  176. * @param bool $secure 是否仅仅通过HTTPS
  177. * @param bool $httponly 仅可通过HTTP访问
  178. * @return void
  179. */
  180. protected function saveCookie(string $name, string $value, int $expire, string $path, string $domain, bool $secure, bool $httponly): void
  181. {
  182. setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
  183. }
  184. }