PHP中 HTTP_HOST 和 SERVER_NAME 的区别

WBOY
Release: 2016-06-23 13:34:56
Original
911 people have browsed it

相同点:
   当满足以下三个条件时,两者会输出相同信息。
  1. 服务器为80端口
  2. apache的conf中ServerName设置正确
  3. HTTP/1.1协议规范

不同点:
 1. 通常情况:
 _SERVER["HTTP_HOST"] 在HTTP/1.1协议规范下,会根据客户端的HTTP请求输出信息。
 _SERVER["SERVER_NAME"] 默认情况下直接输出apache的配置文件httpd.conf中的ServerName值。

2. 当服务器为非80端口时:
  _SERVER["HTTP_HOST"] 会输出端口号,例如:mimiz.cn:8080
 _SERVER["SERVER_NAME"] 会直接输出ServerName值
因此在这种情况下,可以理解为:HTTP_HOST = SERVER_NAME : SERVER_PORT

3. 当配置文件httpd.conf中的ServerName与HTTP/1.0请求的域名不一致时:
httpd.conf配置如下:

ServerName mimiz.cn
ServerAlias www.mimiz.cn

客户端访问域名www.mimiz.cn
_SERVER["HTTP_HOST"] 输出 www.mimiz.cn
_SERVER["SERVER_NAME"] 输出 mimiz.cn

所以,在实际程序中,应尽量使用_SERVER["HTTP_HOST"] ,比较保险和可靠。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template