PHP 中的 HTTP_HOST 和 SERVER_NAME 有什么区别? [英] What is the difference between HTTP_HOST and SERVER_NAME in PHP?
问题描述
$_SERVER['HTTP_HOST']
和 $_SERVER['SERVER_NAME']
在 PHP 中有什么区别?
What is the difference between $_SERVER['HTTP_HOST']
and $_SERVER['SERVER_NAME']
in PHP?
您什么时候会考虑使用一种而不是另一种,为什么?
When would you consider using one over the other and why?
推荐答案
HTTP_HOST
来自 HTTP 请求标头,这是客户端实际用作请求的目标主机"的内容.SERVER_NAME
在服务器配置中定义.使用哪一种取决于你需要它做什么.但是,您现在应该意识到,一个是客户端控制的值,因此在业务逻辑中使用可能不可靠,而另一个是服务器控制的值,它更可靠.但是,您需要确保有问题的网络服务器正确配置了 SERVER_NAME
.以 Apache HTTPD 为例,这里摘自 其文档:
The HTTP_HOST
is obtained from the HTTP request header and this is what the client actually used as "target host" of the request. The SERVER_NAME
is defined in server config. Which one to use depends on what you need it for. You should now however realize that the one is a client-controlled value which may thus not be reliable for use in business logic and the other is a server-controlled value which is more reliable. You however need to ensure that the webserver in question has the SERVER_NAME
correctly configured. Taking Apache HTTPD as an example, here's an extract from its documentation:
如果未指定 ServerName
,则服务器尝试通过对 IP 地址执行反向查找来推断主机名.如果 ServerName
中未指定端口,则服务器将使用传入请求中的端口.为了获得最佳的可靠性和可预测性,您应该使用 ServerName
指令指定显式的主机名和端口.
If no
ServerName
is specified, then the server attempts to deduce the hostname by performing a reverse lookup on the IP address. If no port is specified in theServerName
, then the server will use the port from the incoming request. For optimal reliability and predictability, you should specify an explicit hostname and port using theServerName
directive.
<小时>
更新:在检查Pekka 对您问题的回答后,其中包含指向bobince 的回答,PHP 将始终为 SERVER_NAME
返回 HTTP_HOST
的值,这就是针对几年前我自己的 PHP 4.x + Apache HTTPD 1.2.x 体验,我从 Windows XP 上的当前 XAMPP 环境(Apache HTTPD 2.2.1 和 PHP 5.2.8)中吹走了一些灰尘,启动它,创建一个打印这两个值的 PHP 页面,使用 URLConnection
修改 Host
标头和测试告诉我这确实(不正确)是这种情况.
Update: after checking the answer of Pekka on your question which contains a link to bobince's answer that PHP would always return HTTP_HOST
's value for SERVER_NAME
, which goes against my own PHP 4.x + Apache HTTPD 1.2.x experiences from a couple of years ago, I blew some dust from my current XAMPP environment on Windows XP (Apache HTTPD 2.2.1 with PHP 5.2.8), started it, created a PHP page which prints the both values, created a Java test application using URLConnection
to modify the Host
header and tests taught me that this is indeed (incorrectly) the case.
在第一次怀疑 PHP 并挖掘一些 PHP 错误之后关于该主题的报告,我了解到问题的根源在于所使用的 Web 服务器,当请求 SERVER_NAME
时,它错误地返回了 HTTP Host
标头.所以我深入研究了 Apache HTTPD 错误报告 使用关于主题的各种关键字我终于找到了一个相关错误.这种行为是从 Apache HTTPD 1.3 开始引入的.您需要设置 UseCanonicalName
指令on
在 httpd.conf
中 ServerName
的
条目中(也检查警告文档的底部!).
After first suspecting PHP and digging in some PHP bug reports regarding the subject, I learned that the root of the problem is in web server used, that it incorrectly returned HTTP Host
header when SERVER_NAME
was requested. So I dug into Apache HTTPD bug reports using various keywords regarding the subject and I finally found a related bug. This behaviour was introduced since around Apache HTTPD 1.3. You need to set UseCanonicalName
directive to on
in the <VirtualHost>
entry of the ServerName
in httpd.conf
(also check the warning at the bottom of the document!).
<VirtualHost *>
ServerName example.com
UseCanonicalName on
</VirtualHost>
这对我有用.
总而言之,SERVER_NAME
更可靠,但您依赖服务器配置!
Summarized, SERVER_NAME
is more reliable, but you're dependent on the server config!
这篇关于PHP 中的 HTTP_HOST 和 SERVER_NAME 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!