PHP:$_SERVER 变量:$_SERVER['HTTP_HOST'] 与 $_SERVER['SERVER_NAME']

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13772934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:02:50  来源:igfitidea点击:

PHP: $_SERVER variables: $_SERVER['HTTP_HOST'] vs $_SERVER['SERVER_NAME']

php

提问by Eman

Possible Duplicate:
HTTP_HOST vs. SERVER_NAME

可能重复:
HTTP_HOST 与 SERVER_NAME

What is the difference between $_SERVER['HTTP_HOST']and $_SERVER['SERVER_NAME']??

是有什么区别$_SERVER['HTTP_HOST']$_SERVER['SERVER_NAME']

回答by Mike Brant

$_SERVER['SERVER_NAME']gives the value of the server name as defined in host configuration (i.e for Apache the Apache .conf file).

$_SERVER['SERVER_NAME']给出主机配置中定义的服务器名称的值(即对于 Apache,Apache .conf 文件)。

$_SERVER['HTTP_HOST']gives you the domain name through which the current request is being fulfilled and is more directly related to the request.

$_SERVER['HTTP_HOST']为您提供满足当前请求的域名,并且与请求更直接相关。

HTTP_HOSTis typically more useful in most applications in that it relates directly to the request, whereas SERVER_NAMEcould return whatever value is in the conf file and doesn't tell you anything about the request at all.

HTTP_HOST在大多数应用程序中通常更有用,因为它直接与请求相关,而SERVER_NAME可以返回 conf 文件中的任何值,并且根本不告诉您有关请求的任何信息。

I will give you an example of how HTTP_HOSTmight differ from SERVER_NAME. Say you have an host defined in Apache with ServerName of domain.comand an IP address of 1.2.3.4.

我会给你一个例子,说明HTTP_HOST可能与SERVER_NAME. 假设您在 Apache 中定义了一个主机,其 ServerName 为domain.com,IP 地址为1.2.3.4

Let's look at two incoming request URL's and show the difference between these variables:

让我们看看两个传入的请求 URL 并显示这些变量之间的区别:

http://www.domain.com
HTTP_HOST = www.domain.com
SERVER_NAME = domain.com

http://1.2.3.4
HTTP_HOST = 1.2.3.4
SERVER_NAME = domain.com

So again, HTTP_HOST is tied more to the request, whereas SERVER_NAME is determined by server configuration.

因此,HTTP_HOST 更多地与请求相关联,而 SERVER_NAME 由服务器配置决定。

回答by Frank Farmer

HTTP_HOSTis the Host:header sent by the client. As a result, it might be a little less trustworthy. SERVER_NAMEis determined by your server's configuration, regardless of user input.

HTTP_HOSTHost:客户端发送的标头。因此,它可能不太值得信赖。 无论用户输入如何,SERVER_NAME都由服务器的配置决定

The difference in behavior is subtle. Some good examples are demonstrated here: http://shiflett.org/blog/2006/mar/server-name-versus-http-host

行为上的差异是微妙的。这里展示了一些很好的例子:http: //shiflett.org/blog/2006/mar/server-name-versus-http-host

The docs explain this well

文档很好地解释了这一点

'SERVER_NAME' The name of the server host under which the current script is executing. If the script is running on a virtual host, this will be the value defined for that virtual host.

'HTTP_HOST' Contents of the Host: header from the current request, if there is one.

'SERVER_NAME' 正在执行当前脚本的服务器主机的名称。如果脚本在虚拟主机上运行,​​这将是为该虚拟主机定义的值。

'HTTP_HOST' 主机的内容:来自当前请求的标头,如果有的话。

回答by mpen

HTTP_HOST

HTTP_HOST

Contents of the Host: header from the current request, if there is one.

Host 的内容:来自当前请求的标头,如果有的话。

SERVER_NAME

SERVER_NAME

The name of the server host under which the current script is executing. If the script is running on a virtual host, this will be the value defined for that virtual host.

正在执行当前脚本的服务器主机的名称。如果脚本在虚拟主机上运行,​​这将是为该虚拟主机定义的值。

http://php.net/manual/en/reserved.variables.server.php

http://php.net/manual/en/reserved.variables.server.php