Blogger Information
Blog 9
fans 0
comment 0
visits 45588
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP获取当前域名$_SERVER['HTTP_HOST']和$_SERVER['SERVER_NAME']的区别
Fire的博客
Original
1848 people have browsed it

PHP获取当前域名$_SERVER['HTTP_HOST']和$_SERVER['SERVER_NAME']的区别    

http://blog.snsgou.com/post-749.html

http://www.w3school.com.cn/php/func_string_stripos.asp

开发站群软件,用到了根据访问域名判断子站点的相关问题,PHP获取当前域名有两个变量 $_SERVER['HTTP_HOST'] 和 $_SERVER['SERVER_NAME'],两者的区别以及哪个更加可靠?

 

相同点:

当满足以下三个条件时,两者会输出相同信息。

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"] 会输出端口号,例如:snsgou.com:8080

$_SERVER["SERVER_NAME"] 会直接输出ServerName值

因此在这种情况下,可以理解为:$_SERVER['HTTP_HOST'] = $_SERVER['SERVER_NAME'] : $_SERVER['SERVER_PORT']

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

httpd.conf配置如下:

1    <virtualhost *>    

2        ServerName snsgou.com    

3        ServerAlias blog.snsgou.com    

4    </virtualhost>    

***端访问域名 blog.snsgou.com

$_SERVER["HTTP_HOST"] 输出 blog.snsgou.com
$_SERVER["SERVER_NAME"] 输出 snsgou.com

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

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments