From 1e909984eaa78cea416cc5f7c9a1d9cf2b5bcea2 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 23 Mar 2017 11:58:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BCookie=E7=B1=BB=E7=9A=84get?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E6=94=AF=E6=8C=81=E8=8E=B7=E5=8F=96=E5=85=A8?= =?UTF-8?q?=E9=83=A8=20=E6=94=B9=E8=BF=9BRequest=E7=B1=BB=E7=9A=84cookie?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Cookie.php | 25 +++++++++++++++++++------ library/think/Request.php | 4 +++- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/library/think/Cookie.php b/library/think/Cookie.php index 93edb8d7..3205fcd9 100644 --- a/library/think/Cookie.php +++ b/library/think/Cookie.php @@ -135,22 +135,35 @@ class Cookie * @param string|null $prefix cookie前缀 * @return mixed */ - public static function get($name, $prefix = null) + public static function get($name = '', $prefix = null) { !isset(self::$init) && self::init(); $prefix = !is_null($prefix) ? $prefix : self::$config['prefix']; - $name = $prefix . $name; - if (isset($_COOKIE[$name])) { - $value = $_COOKIE[$name]; + $key = $prefix . $name; + + if ('' == $name) { + // 获取全部 + if ($prefix) { + $value = []; + foreach ($_COOKIE as $k => $val) { + if (0 === strpos($k, $prefix)) { + $value[$k] = $val; + } + } + } else { + $value = $_COOKIE; + } + } elseif (isset($_COOKIE[$key])) { + $value = $_COOKIE[$key]; if (0 === strpos($value, 'think:')) { $value = substr($value, 6); $value = json_decode($value, true); array_walk_recursive($value, 'self::jsonFormatProtect', 'decode'); } - return $value; } else { - return; + $value = null; } + return $value; } /** diff --git a/library/think/Request.php b/library/think/Request.php index efc19c35..97599144 100644 --- a/library/think/Request.php +++ b/library/think/Request.php @@ -802,10 +802,12 @@ class Request public function cookie($name = '', $default = null, $filter = '') { if (empty($this->cookie)) { - $this->cookie = $_COOKIE; + $this->cookie = Cookie::get(); } if (is_array($name)) { return $this->cookie = array_merge($this->cookie, $name); + } elseif (!empty($name)) { + $name = Cookie::prefix() . $name; } return $this->input($this->cookie, $name, $default, $filter); }