$_SERVER[‘HTTP_HOST’]与$_SERVER[‘SERVER_NAME’]的区别

知更鸟 2019年1月8日21:38:22WordPress 开发来源:网络评论205阅读模式

PHP中的$_SERVER['HTTP_HOST']与$_SERVER['SERVER_NAME']的区别文章源自知更鸟-https://zmingcx.com/http_host-v-erver_name.html

通过:$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']可以获取域名及请求的URL。文章源自知更鸟-https://zmingcx.com/http_host-v-erver_name.html

1、变量HTTP_HOST和SERVER_NAME都是获取当前域名文章源自知更鸟-https://zmingcx.com/http_host-v-erver_name.html

2、当满足以下3个条件的时候,两者会输出相同的信息文章源自知更鸟-https://zmingcx.com/http_host-v-erver_name.html

A、服务器端口号为80文章源自知更鸟-https://zmingcx.com/http_host-v-erver_name.html

B、Apache中的conf中的ServerName设置正确文章源自知更鸟-https://zmingcx.com/http_host-v-erver_name.html

C、HTTP/1.1协议规范文章源自知更鸟-https://zmingcx.com/http_host-v-erver_name.html

3、不同点文章源自知更鸟-https://zmingcx.com/http_host-v-erver_name.html

A、$_SERVER['HTTP_HOST']:在HTTP/1.1协议下,会根据客户端的HTTP请求输出信息;文章源自知更鸟-https://zmingcx.com/http_host-v-erver_name.html

$_SERVER['SERVER_NAME']:默认情况下会直接输出Apache的配置文件httpd.conf中的ServerName的值文章源自知更鸟-https://zmingcx.com/http_host-v-erver_name.html

B、当服务器的端口号不是80时文章源自知更鸟-https://zmingcx.com/http_host-v-erver_name.html

$_SERVER['HTTP_HOST']会输出端口号,例如“www.funwall.cn:8080”文章源自知更鸟-https://zmingcx.com/http_host-v-erver_name.html

$_SERVER['SERVER_NAME']直接输出ServerName的值,例如“www.funwall.cn”文章源自知更鸟-https://zmingcx.com/http_host-v-erver_name.html

在这种情况下,可以理解成HTTP_HOST = SERVER_NAME:SERVRE_PORT文章源自知更鸟-https://zmingcx.com/http_host-v-erver_name.html

C、当配置文件httpd.conf中的ServerName与HTTP/1.0下的域名不一致的时候,假如httpd.conf配置如下:文章源自知更鸟-https://zmingcx.com/http_host-v-erver_name.html

ServerName funwall.cn文章源自知更鸟-https://zmingcx.com/http_host-v-erver_name.html

ServerAlias www.funwall.cn文章源自知更鸟-https://zmingcx.com/http_host-v-erver_name.html

客户端访问域名:www.funwall.cn文章源自知更鸟-https://zmingcx.com/http_host-v-erver_name.html

$_SERVER[HTTP_HOST]输出:www.funwall.cn文章源自知更鸟-https://zmingcx.com/http_host-v-erver_name.html

$_SERVER[SERVER_NAME]输出:funwall.cn文章源自知更鸟-https://zmingcx.com/http_host-v-erver_name.html

因此,在实际中应该用$_SERVER[HTTP_HOST]获取域名比较保险文章源自知更鸟-https://zmingcx.com/http_host-v-erver_name.html

weinxin
版权声明
本站原创文章转载请注明文章出处及链接,谢谢合作!
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定