diff --git a/helper.php b/helper.php index 8bce6e92..1b8def16 100644 --- a/helper.php +++ b/helper.php @@ -523,3 +523,16 @@ if (!function_exists('halt')) { throw new \think\exception\HttpResponseException(new Response); } } + +if (!function_exists('token')) { + /** + * 生成表单令牌 + * @param string $name 令牌名称 + * @param mixed $type 令牌生成方法 + */ + function token($name = '__token__', $type = 'md5') + { + $token = Request::instance()->token($name, $type); + return ''; + } +} diff --git a/library/think/Request.php b/library/think/Request.php index 0d7610fd..4449b97f 100644 --- a/library/think/Request.php +++ b/library/think/Request.php @@ -1416,7 +1416,9 @@ class Request { $type = is_callable($type) ? $type : 'md5'; $token = call_user_func($type, $_SERVER['REQUEST_TIME_FLOAT']); - + if ($this->isAjax()) { + header($name . ': ' . $token); + } Session::set($name, $token); return $token; }