Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

122 Zeilen
3.4 KiB

  1. <?php
  2. namespace Kuxin;
  3. /**
  4. * Class Cookie
  5. *
  6. * @package Kuxin
  7. * @author Pakey <pakey@qq.com>
  8. */
  9. class Cookie
  10. {
  11. /**
  12. * 默认配置
  13. *
  14. * @var array
  15. */
  16. protected static $option = [
  17. 'prefix' => 'kuxin_',
  18. // cookie 保存时间
  19. 'expire' => 2592000,
  20. // cookie 保存路径
  21. 'path' => '/',
  22. // cookie 有效域名
  23. 'domain' => '',
  24. // cookie 启用安全传输
  25. 'secure' => false,
  26. // httponly设置
  27. 'httponly' => '',
  28. ];
  29. public function __construct(array $config = [])
  30. {
  31. $this->init($config);
  32. }
  33. /**
  34. * 初始化
  35. *
  36. * @param array $config
  37. */
  38. public static function init(array $config = []): void
  39. {
  40. $config = [
  41. 'prefix' => Input::param('prefix', 'string', Config::get('cookie_prefix', 'PTCMS_'), $config),
  42. // cookie 保存时间
  43. 'expire' => Input::param('expire', 'int', Config::get('cookie_expire', 2592000), $config),
  44. // cookie 保存路径
  45. 'path' => Input::param('path', 'string', Config::get('cookie_path', '/'), $config),
  46. // cookie 有效域名
  47. 'domain' => Input::param('domain', 'string', Config::get('cookie_domain', ''), $config),
  48. // cookie 启用安全传输
  49. 'secure' => Input::param('secure', 'string', Config::get('cookie_secure', false), $config),
  50. // httponly设置
  51. 'httponly' => Input::param('httponly', 'string', Config::get('cookie_httponly', ''), $config),
  52. ];
  53. self::$option = array_merge(self::$option, $config);
  54. }
  55. /**
  56. * 获取
  57. *
  58. * @param $name
  59. * @param mixed $default
  60. * @return mixed
  61. */
  62. public static function get(string $name, $default = null)
  63. {
  64. $fullname = self::$option['prefix'] . $name;
  65. return $_COOKIE[$fullname] ?? ((is_callable($default) ? $default($name) : $default));
  66. }
  67. /**
  68. * 设置cookie
  69. * @param string $name
  70. * @param string $value
  71. * @param array|null $option
  72. */
  73. public static function set(string $name, string $value = '', $option = null): void
  74. {
  75. if (!is_null($option)) {
  76. if (is_numeric($option))
  77. $option = ['expire' => $option];
  78. elseif (is_string($option))
  79. parse_str($option, $option);
  80. $config = array_merge(self::$option, array_change_key_case($option));
  81. } else {
  82. $config = self::$option;
  83. }
  84. $name = self::$option['prefix'] . $name;
  85. $expire = !empty($config['expire']) ? time() + $config['expire'] : 0;
  86. setcookie($name, $value, $expire, $config['path'], $config['domain']);
  87. $_COOKIE[$name] = $value;
  88. }
  89. /**
  90. * 删除单个
  91. *
  92. * @param $name
  93. */
  94. public static function remove(string $name): void
  95. {
  96. $name = self::$option['prefix'] . $name;
  97. setcookie($name, '', time() - 3600, self::$option['path'], self::$option['domain']);
  98. // 删除指定cookie
  99. unset($_COOKIE[$name]);
  100. }
  101. /**
  102. * 清空
  103. */
  104. public static function clear(): void
  105. {
  106. foreach ($_COOKIE as $key => $val) {
  107. if (0 === stripos($key, self::$option['prefix'])) {
  108. setcookie($key, '', time() - 3600, self::$option['path'], self::$option['domain']);
  109. unset($_COOKIE[$key]);
  110. }
  111. }
  112. }
  113. }