$_SERVER['HTTP_HOST'] 也包含端口号 =/
我不知道也许这是一个错误。
我的服务器上有 2 个虚拟主机。
virtualhost1.com virtualhost2.com
如果我使用端口 80 (virtualhost1.com:80) 打开 virtualhost1.com
$_SERVER['HTTP_HOST']='virtualhost1.com';
但如果我打开 virtualhost2.com:80
$_SERVER['HTTP_HOST']='virtualhost2.com:80'; // NOTE: with port number
我能知道为什么吗?
I don't know maybe it's a bug.
I have 2 virutalhosts on my server.
virtualhost1.com
virtualhost2.com
if i open virtualhost1.com with port 80 (virtualhost1.com:80)
$_SERVER['HTTP_HOST']='virtualhost1.com';
but if i open virtualhost2.com:80
$_SERVER['HTTP_HOST']='virtualhost2.com:80'; // NOTE: with port number
Can I know why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
$_SERVER['HTTP_HOST']
的值直接取自Host:
HTTP 请求标头。看来发出请求的客户正在以这种方式填写它。我建议使用
$_SERVER['SERVER_NAME']
代替,因为它的值将从您的虚拟主机配置中设置。但是,正如 Flimm 在下面指出的那样,即使SERVER_NAME
的可靠性仍然依赖于服务器配置(请查看 此答案以获取更多信息)。The value of
$_SERVER['HTTP_HOST']
is taken directly from theHost:
HTTP request header. It appears the requesting client is filling it in that way.I suggest using
$_SERVER['SERVER_NAME']
instead as its value will be set from your virtual host config. However, as Flimm notes below, even the reliability ofSERVER_NAME
can still be dependent on server config (check out this answer for more info on that).以下函数始终返回没有端口的真实主机(用户输入的主机),并且它几乎是可靠的:
Following function always return real host (user typed host) without port and it's almost reliable: