PHP中的HTTP_HOST和SERVER_NAME有什么区别? [英] What is the difference between HTTP_HOST and SERVER_NAME in PHP?

查看:125
本文介绍了PHP中的HTTP_HOST和SERVER_NAME有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP中的HTTP_HOSTSERVER_NAME有什么区别?

What is the difference between HTTP_HOST and SERVER_NAME in PHP?

其中:

  • HTTP_POST === $_SERVER['HTTP_HOST']
  • SERVER_NAME === $_SERVER['SERVER_NAME']
  • HTTP_POST === $_SERVER['HTTP_HOST']
  • SERVER_NAME === $_SERVER['SERVER_NAME']

您何时会考虑使用另一种,为什么呢?

When would you consider using one over the other and why?

推荐答案

HTTP_HOST是从SERVER_NAME在服务器配置中定义.使用哪种取决于您的需要.但是,您现在应该意识到,一个是客户端控制的值,因此在业务逻辑中使用可能不可靠,而另一个是服务器控制的值,因此更可靠.但是,您需要确保所讨论的Web服务器已正确配置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 the ServerName, 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 the ServerName directive.


更新:检查佩卡对您问题的回答后,其中包含指向 bobince的答案:PHP将始终为SERVER_NAME返回HTTP_HOST的值,这与我自己的PHP 4.x + Apache HTTPD背道而驰几年前的1.2.x经验,我从Windows XP(带有PHP 5.2.8的Apache HTTPD 2.2.1)上的当前XAMPP环境中清除了一些灰尘,启动了它,创建了一个同时打印两个值的PHP页面, Java测试应用程序,它使用 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.confServerName<VirtualHost>条目(也请检查文档!).

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆