From fd99c07e106ad30a40e5f6c4be1a572d6678ba0b Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 15 Aug 2016 12:37:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0token=E5=8A=A9=E6=89=8B?= =?UTF-8?q?=E5=87=BD=E6=95=B0=20=E7=94=A8=E4=BA=8E=E5=9C=A8=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=E5=BF=AB=E9=80=9F=E6=98=BE=E7=A4=BA=E4=BB=A4=E7=89=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helper.php | 13 +++++++++++++ library/think/Request.php | 4 +++- 2 files changed, 16 insertions(+), 1 deletion(-) 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; }