From 4c046575b68cea910afab01edbfbbef38eb1a149 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 27 Apr 2018 17:13:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BRequest=E7=B1=BB=E7=9A=84host?= =?UTF-8?q?=E6=96=B9=E6=B3=95=20=E6=94=AF=E6=8C=81=E4=BB=85=E4=BB=85?= =?UTF-8?q?=E8=8E=B7=E5=8F=96host=E8=80=8C=E4=B8=8D=E5=8C=85=E5=90=AB?= =?UTF-8?q?=E7=AB=AF=E5=8F=A3=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Request.php | 10 +++++++--- library/think/Route.php | 2 +- library/think/Url.php | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/library/think/Request.php b/library/think/Request.php index 5710c5cb..8a0dc80d 100644 --- a/library/think/Request.php +++ b/library/think/Request.php @@ -1342,14 +1342,18 @@ class Request /** * 当前请求的host * @access public + * @param bool $strict true 仅仅获取HOST * @return string */ - public function host() + public function host($strict = false) { if (isset($_SERVER['HTTP_X_REAL_HOST'])) { - return $_SERVER['HTTP_X_REAL_HOST']; + $host = $_SERVER['HTTP_X_REAL_HOST']; + } else { + $host = $this->server('HTTP_HOST'); } - return $this->server('HTTP_HOST'); + + return true === $strict ? strstr($host, ':', true) : $host; } /** diff --git a/library/think/Route.php b/library/think/Route.php index f940d28b..99ffa706 100644 --- a/library/think/Route.php +++ b/library/think/Route.php @@ -737,7 +737,7 @@ class Route $rules = self::$rules['domain']; // 开启子域名部署 支持二级和三级域名 if (!empty($rules)) { - $host = $request->host(); + $host = $request->host(true); if (isset($rules[$host])) { // 完整域名或者IP配置 $item = $rules[$host]; diff --git a/library/think/Url.php b/library/think/Url.php index 53a545f9..bb697192 100644 --- a/library/think/Url.php +++ b/library/think/Url.php @@ -239,7 +239,7 @@ class Url $rootDomain = Config::get('url_domain_root'); if (true === $domain) { // 自动判断域名 - $domain = Config::get('app_host') ?: $request->host(); + $domain = Config::get('app_host') ?: $request->host(true); $domains = Route::rules('domain'); if ($domains) {