增加token助手函数 用于在页面快速显示令牌

This commit is contained in:
thinkphp
2016-08-15 12:37:10 +08:00
parent 1440277478
commit fd99c07e10
2 changed files with 16 additions and 1 deletions

View File

@@ -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 '<input type="hidden" name="' . $name . '" value="' . $token . '" />';
}
}

View File

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