PHP 动态识别子域的最佳方法是什么?
我已经配置了 *.mydomain.com 的通配符 DNS,并且一切正常。我的问题是我应该依靠哪一个来识别客户端子域请求?
- $_SERVER["HTTP_HOST"]
- $_SERVER["SERVER_NAME"]
- $_SERVER["SCRIPT_URI"]
它们似乎都包含我想要的子域部分,但在阅读 Chris 的这篇文章后: http://shiflett.org/blog/2006/mar/server-name-versus-http-host,我在海上迷路了,似乎没有安全的方法可以做到这一点?
关于安全地完成这项任务有什么想法吗?您更喜欢哪种方法?
更新:抱歉,我的意思是这篇文章: http://shiflett .org/blog/2006/mar/server-name-versus-http-host
I have configured the wildcard DNS of *.mydomain.com and it's all working properly. My question is which of these should I rely on identifying client subdomain requests?
- $_SERVER["HTTP_HOST"]
- $_SERVER["SERVER_NAME"]
- $_SERVER["SCRIPT_URI"]
They all seem to contain the subdomain part I want but after reading this article by Chris: http://shiflett.org/blog/2006/mar/server-name-versus-http-host, I'm lost at sea and there appears to be no safe way to do this?
Any idea on accomplishing this task securely? Which approach would you prefer?
Update: sorry, I meant this post: http://shiflett.org/blog/2006/mar/server-name-versus-http-host
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
HTTP_HOST 直接来自 HOST 标头。 Apache 不会以任何方式清理它。即使对于非通配符设置,配置中的第一个虚拟主机也会收到与您配置的任何虚拟主机都不匹配的 HOST 标头的请求,因此您必须小心处理。像对待任何其他用户数据一样对待它。使用前适当过滤。
HTTP_HOST comes directly from the HOST header. Apache does not clean it up in any way. Even for non-wildcard setups, your first virtualhost in your config will receive a request for a HOST header that doesn't match any of your configured vhosts, so you have to be careful with it. Treat it like any other user data. Filter it appropriately before using it.
我建议你获取当前页面的 url,然后使用正则表达式进行检查。请务必忽略链接 www、www2 等内容。
I'd suggest that you get the current page url, then use a regular expression to check. Be sure to ignore things link www, www2, etc.
您可以使用任何一个,但大多数使用 HTTP_HOST。
您不必担心此处的“安全”,因为您允许子域使用通配符。您将无法阻止用户进入“威胁”子域并向您的服务器发送请求。
如果您想禁止某些子域,那么您有多种选择,但那是另一个问题。
You can use any but most use HTTP_HOST.
You don't have to worry about 'security' here since you allow a wildcard for your subdomains. You won't be able to stop a user from entering a 'threatening' subdomain and sending a request to your server.
If you want to disallow certain subdomains then you have several options but thats a different question.
始终返回一个数组,如果没有子域则可以为空。您还应该确保注意到,这可能会返回
www
作为数组值,并且无论如何都会链接到您的根域。Returns an array always, and can be empty if there is no sub domain. You should also make sure to notice that this could return
www
as an array value and that would link to your root domain anyway.这么一个小问题就说太多了。
每个人都说这很危险,但没有人费心去写一个解决方案,就像简单的那样
Too much talk of such a little problem.
Everyone says its dangerous but noone bother to write a solution, as simple as