From a25b16b0cf283bad25477b7f960c10a40b871c62 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 19 Oct 2016 15:16:31 +0800 Subject: [PATCH] =?UTF-8?q?Session=E7=B1=BB=E5=A2=9E=E5=8A=A0flash?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E7=94=A8=E4=BA=8E=E8=AE=BE=E7=BD=AE=E4=B8=8B?= =?UTF-8?q?=E4=B8=80=E6=AC=A1=E8=AF=B7=E6=B1=82=E6=9C=89=E6=95=88=E7=9A=84?= =?UTF-8?q?=E5=80=BC=20flush=E6=96=B9=E6=B3=95=E7=94=A8=E4=BA=8E=E6=B8=85?= =?UTF-8?q?=E7=A9=BA=E5=BD=93=E5=89=8D=E8=AF=B7=E6=B1=82=E6=9C=89=E6=95=88?= =?UTF-8?q?=E7=9A=84=E5=80=BC=20=E5=A2=9E=E5=8A=A0push=E6=96=B9=E6=B3=95?= =?UTF-8?q?=20Redirect=20response=E7=B1=BB=E7=9A=84with=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=E8=B0=83=E7=94=A8session=E7=B1=BB=E7=9A=84fl?= =?UTF-8?q?ash=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Response.php | 3 ++ library/think/Session.php | 58 ++++++++++++++++++++++++++++- library/think/response/Redirect.php | 4 +- 3 files changed, 61 insertions(+), 4 deletions(-) diff --git a/library/think/Response.php b/library/think/Response.php index 8b62e419..a202ce76 100644 --- a/library/think/Response.php +++ b/library/think/Response.php @@ -130,6 +130,9 @@ class Response // 监听response_end Hook::listen('response_end', $this); + + // 清空当次请求有效的数据 + Session::flush(); } /** diff --git a/library/think/Session.php b/library/think/Session.php index e8f56fd9..822f0453 100644 --- a/library/think/Session.php +++ b/library/think/Session.php @@ -196,8 +196,42 @@ class Session } /** - * 删除session数据 + * session设置 下一次请求有效 * @param string $name session名称 + * @param mixed $value session值 + * @param string|null $prefix 作用域(前缀) + * @return void + */ + public static function flash($name, $value) + { + self::set($name, $value); + if (!self::has('__flash__.__time__')) { + self::set('__flash__.__time__', $_SERVER['REQUEST_TIME_FLOAT']); + } + self::push('__flash__', $name); + } + + /** + * 清空当前请求的session数据 + * @return void + */ + public static function flush() + { + $item = self::get('__flash__'); + + if (!empty($item)) { + $time = $item['__time__']; + if ($_SERVER['REQUEST_TIME_FLOAT'] > $time) { + unset($item['__time__']); + self::delete($item); + self::set('__flash__', []); + } + } + } + + /** + * 删除session数据 + * @param string|array $name session名称 * @param string|null $prefix 作用域(前缀) * @return void */ @@ -205,7 +239,11 @@ class Session { empty(self::$init) && self::boot(); $prefix = !is_null($prefix) ? $prefix : self::$prefix; - if (strpos($name, '.')) { + if (is_array($name)) { + foreach ($name as $key) { + self::delete($key, $prefix); + } + } elseif (strpos($name, '.')) { list($name1, $name2) = explode('.', $name); if ($prefix) { unset($_SESSION[$prefix][$name1][$name2]); @@ -256,6 +294,22 @@ class Session } } + /** + * 添加数据到一个session数组 + * @param string $key + * @param mixed $value + * @return void + */ + public static function push($key, $value) + { + $array = self::get($key); + if (is_null($array)) { + $array = []; + } + $array[] = $value; + self::set($key, $array); + } + /** * 启动session * @return void diff --git a/library/think/response/Redirect.php b/library/think/response/Redirect.php index 82a5fe32..0d65e5e3 100644 --- a/library/think/response/Redirect.php +++ b/library/think/response/Redirect.php @@ -53,10 +53,10 @@ class Redirect extends Response { if (is_array($name)) { foreach ($name as $key => $val) { - Session::set($key, $val); + Session::flash($key, $val); } } else { - Session::set($name, $value); + Session::flash($name, $value); } return $this; }