为什么 $_SERVER['HTTP_HOST'] 有时返回大写?
我使用 $_SERVER['HTTP_HOST']
检查在正确域上运行的脚本,并在非法域上运行时发送电子邮件。问题是有时我会收到脚本在不正确的域上运行的错误警报。这是一个错误警报,$_SERVER['HTTP_HOST']
为大写。
首先,为什么会出现这种情况? 其次,检查 $_SERVER['HTTP_HOST']
是否安全,或者是否有更好的原因?
注意:我没有在代码中将值转换为小写。
I use $_SERVER['HTTP_HOST']
to check my script running on correct domain and send an email when it runs on illegal domain. The problem is that sometimes I receive incorrect alerts that script run on incorrect domain. It is a false alert with $_SERVER['HTTP_HOST']
in uppercase.
First, why this happen?
Second, is this safe to check $_SERVER['HTTP_HOST']
or is there a better why?
Note: I didn't convert value to lowercase in my code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
$_SERVER['HTTP_HOST'] 是访问脚本的主机名。因此,如果有人访问 http://WWW.EXAMPLE.COM/ ,它将是大写的。
检查起来很安全,只需先使用 strtolower() 将其转换为小写即可。
编辑:我说检查是安全的,但有一个警告:使用 HTTP/1.0 的用户代理不会提供 Host: 标头,因此 $_SERVER['HTTP_HOST'] 将被取消设置。这种情况不太可能发生,但如果您想防范这种情况,请确保针对这种可能性进行编码。
$_SERVER['HTTP_HOST'] is the hostname that the script was accessed by. So, if someone goes to http://WWW.EXAMPLE.COM/ it'll be uppercase.
It's safe to check, just convert it to lowercase with strtolower() first.
Edit: I say it's safe to check, but there is one caveat: user agents using HTTP/1.0 won't supply a Host: header, so $_SERVER['HTTP_HOST'] will be unset. This is very unlikely to happen, but if you want to protect against it make sure you code for that possibility.