PHP:$ _SERVER变量:$ _SERVER ['HTTP_HOST']与$ _SERVER ['SERVER_NAME'] [英] PHP: $_SERVER variables: $_SERVER['HTTP_HOST'] vs $_SERVER['SERVER_NAME']
问题描述
可能重复:
HTTP_HOST与服务器名
Possible Duplicate:
HTTP_HOST vs. SERVER_NAME
$_SERVER['HTTP_HOST']
和$_SERVER['SERVER_NAME']
??
推荐答案
$_SERVER['SERVER_NAME']
给出主机配置中定义的服务器名称的值(即Apache的Apache .conf文件).
$_SERVER['SERVER_NAME']
gives the value of the server name as defined in host configuration (i.e for Apache the Apache .conf file).
$_SERVER['HTTP_HOST']
为您提供用于满足当前请求的域名,该域名与该请求更直接相关.
$_SERVER['HTTP_HOST']
gives you the domain name through which the current request is being fulfilled and is more directly related to the request.
HTTP_HOST
通常在大多数应用程序中更有用,因为它直接与请求有关,而SERVER_NAME
可以返回conf文件中的任何值,并且根本不告诉您有关该请求的任何信息.
HTTP_HOST
is typically more useful in most applications in that it relates directly to the request, whereas SERVER_NAME
could return whatever value is in the conf file and doesn't tell you anything about the request at all.
我将为您提供一个示例,说明HTTP_HOST
与SERVER_NAME
可能有何不同.假设您有一个在Apache中定义的主机,服务器名称为domain.com
,IP地址为1.2.3.4
.
I will give you an example of how HTTP_HOST
might differ from SERVER_NAME
. Say you have an host defined in Apache with ServerName of domain.com
and an IP address of 1.2.3.4
.
让我们看一下两个传入的请求URL,并显示这些变量之间的区别:
Let's look at two incoming request URL's and show the difference between these variables:
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
同样,HTTP_HOST与请求的绑定更多,而SERVER_NAME由服务器配置确定.
So again, HTTP_HOST is tied more to the request, whereas SERVER_NAME is determined by server configuration.
这篇关于PHP:$ _SERVER变量:$ _SERVER ['HTTP_HOST']与$ _SERVER ['SERVER_NAME']的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!