PHP教程:HTTP1.0协议下HTTP_HOST为空的根本原因
2012-05-16 17:51
357 查看
昨天xuepeng师兄提出一个问题是PHP在获取 $_SERVER['HTTP_HOST']为空,经过我翻看RFC文档以及测试,得出结论如下:
在http 1.1中,host字段是不能为空的,如果为空,服务器会认为是bad request
但是在http 1.0中,host字段是可以为空的. 如:
其中,主机的index.php只是var_dump($_SERVER['HTTP_HOST']);
可以看到,当你指明使用http 1.0协议的时候,请求正常,返回结果是false;
但是如果你指明协议是http 1.1 :
则结果是400 bad request;
究其原因是因为在HTTP1.0的时候,并没有设想到现在有这么多服务器共用一个IP的情况(virtual host),而在HTTP1.1的时候,加入了对多个HOST共用一个IP的支持.
以下文字摘自RFC2616:
14.23 Host
The Host request-header field specifies the Internet host and port
number of the resource being requested,as obtained from the original
URI given by the user or referring resource (generally an HTTP URL,
Fielding,et al. Standards Track [Page 128]
RFC 2616 HTTP/1.1 June 1999
as described in section 3.2.2). The Host field value MUST represent
the naming authority of the origin server or gateway given by the
original URL. This allows the origin server or gateway to
differentiate between internally-ambiguous URLs,such as the root “/”
URL of a server for multiple host names on a single IP address.
Host =“Host” “:” host [ ":" port ] ; Section 3.2.2
A “host” without any trailing port information implies the default
port for the service requested (e.g.,“80″ for an HTTP URL). For
example,a request on the origin server for
would properly include:
GET /pub/WWW/ HTTP/1.1
Host: www.w3.org
A client MUST include a Host header field in all HTTP/1.1 request
messages . If the requested URI does not include an Internet host
name for the service being requested,then the Host header field MUST
be given with an empty value. An HTTP/1.1 proxy MUST ensure that any
request message it forwards does contain an appropriate Host header
field that identifies the service being requested by the proxy. All
Internet-based HTTP/1.1 servers MUST respond with a 400 (Bad Request)
status code to any HTTP/1.1 request message which lacks a Host header
field.
以下省略…..
后记: 虽然HTTP_HOST不能缺失,但是可以为空值 (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.23):
If the requested URI does not include an Internet host
name for the service being requested,then the Host header field MUST be
given with an empty value.
作者: Laruence
原文链接:http://www.laruence.com/2008/08/28/483.html
在http 1.1中,host字段是不能为空的,如果为空,服务器会认为是bad request
但是在http 1.0中,host字段是可以为空的. 如:
1 | <?php |
2 | $fp = fsockopen ( "localhost" ,80, $errno , $errstr ,30); |
3 |
4 | $header = "GET /index.php" ; |
5 | $header .= " HTTP/1.0\r\n" ; |
6 |
7 | $header .= "Connection:Close\r\n\r\n" ; |
8 | fwrite( $fp , $header ); |
9 | echo fread ( $fp ,1024); |
10 | fclose( $fp ); |
11 | ?> |
可以看到,当你指明使用http 1.0协议的时候,请求正常,返回结果是false;
但是如果你指明协议是http 1.1 :
1 | <?php |
2 | $fp = fsockopen ( "localhost" ,80, $errno , $errstr ,30); |
3 |
4 | $header = "GET /index.php" ; |
5 | $header .= " HTTP/1.1\r\n" ; |
6 |
7 | $header .= "Connection:Close\r\n\r\n" ; |
8 | fwrite( $fp , $header ); |
9 | echo fread ( $fp ,1024); |
10 | fclose( $fp ); |
11 | ?> |
究其原因是因为在HTTP1.0的时候,并没有设想到现在有这么多服务器共用一个IP的情况(virtual host),而在HTTP1.1的时候,加入了对多个HOST共用一个IP的支持.
以下文字摘自RFC2616:
14.23 Host
The Host request-header field specifies the Internet host and port
number of the resource being requested,as obtained from the original
URI given by the user or referring resource (generally an HTTP URL,
Fielding,et al. Standards Track [Page 128]
RFC 2616 HTTP/1.1 June 1999
as described in section 3.2.2). The Host field value MUST represent
the naming authority of the origin server or gateway given by the
original URL. This allows the origin server or gateway to
differentiate between internally-ambiguous URLs,such as the root “/”
URL of a server for multiple host names on a single IP address.
Host =“Host” “:” host [ ":" port ] ; Section 3.2.2
A “host” without any trailing port information implies the default
port for the service requested (e.g.,“80″ for an HTTP URL). For
example,a request on the origin server for
would properly include:
GET /pub/WWW/ HTTP/1.1
Host: www.w3.org
A client MUST include a Host header field in all HTTP/1.1 request
messages . If the requested URI does not include an Internet host
name for the service being requested,then the Host header field MUST
be given with an empty value. An HTTP/1.1 proxy MUST ensure that any
request message it forwards does contain an appropriate Host header
field that identifies the service being requested by the proxy. All
Internet-based HTTP/1.1 servers MUST respond with a 400 (Bad Request)
status code to any HTTP/1.1 request message which lacks a Host header
field.
以下省略…..
后记: 虽然HTTP_HOST不能缺失,但是可以为空值 (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.23):
If the requested URI does not include an Internet host
name for the service being requested,then the Host header field MUST be
given with an empty value.
作者: Laruence
原文链接:http://www.laruence.com/2008/08/28/483.html
相关文章推荐
- PHP教程:HTTP1.0协议下HTTP_HOST为空的根本原因
- PHPStorm+XDebug进行调试图文教程以及解析wamp的php.ini设置不生效的原因以及Interpreter is not specified or invalid解决办法(调试不生效的原因,两处红色部分)
- http协议get、post请求分析及用HttpRequester测试的报错及可能原因
- php中http协议的一些基本知识
- HTTP协议分析系列(五)------php+socket编程发送http请求
- http协议监测调试的“瑞士军刀” LiveHttpHeaders 安装使用教程
- PHP中$_SERVER中HTTP_HOST和SERVER_NAME的区别
- PHP获取当前域名$_SERVER['HTTP_HOST']和$_SERVER['SERVER_NA
- HTTP协议实践篇--使用Fiddler与后台php交互
- php $_SERVER中的SERVER_NAME 和HTTP_HOST的区别
- php http 协议下载类(自家用)
- PHP SERVER_NAME和HTTP_HOST的区别
- PHP的$_SERVER['HTTP_HOST']获取服务器地址功能详解,$_SERVER['HTTP_X_FORWARDED_HOST']
- php http_build_query 实现教程
- PHP中Http协议post请求参数
- PHP中的http协议
- PHP自学之路--- HTTP协议深度剖析
- PHP中 HTTP_HOST 和 SERVER_NAME 的区别
- http协议中apache隐藏头部版本信息,PHP版本信息隐藏
- httpclient org.apache.http.NoHttpResponseException: host:端口 failed to respond 错误原因和解决方法