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; }