PHP 动态识别子域的最佳方法是什么?

发布于 2024-08-28 09:56:13 字数 653 浏览 4 评论 0原文

我已经配置了 *.mydomain.com 的通配符 DNS,并且一切正常。我的问题是我应该依靠哪一个来识别客户端子域请求?

  1. $_SERVER["HTTP_HOST"]
  2. $_SERVER["SERVER_NAME"]
  3. $_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?

  1. $_SERVER["HTTP_HOST"]
  2. $_SERVER["SERVER_NAME"]
  3. $_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

墨小墨 2024-09-04 09:56:13

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.

弥枳 2024-09-04 09:56:13

我建议你获取当前页面的 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.

骑趴 2024-09-04 09:56:13

您可以使用任何一个,但大多数使用 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.

清欢 2024-09-04 09:56:13
$subdomain = explode('.', $_SERVER['HTTP_HOST'], -2);

始终返回一个数组,如果没有子域则可以为空。您还应该确保注意到,这可能会返回 www 作为数组值,并且无论如何都会链接到您的根域。

$subdomain = explode('.', $_SERVER['HTTP_HOST'], -2);

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.

久而酒知 2024-09-04 09:56:13

这么一个小问题就说太多了。
每个人都说这很危险,但没有人费心去写一个解决方案,就像简单的那样

$mydomain='example.com';
$subdomain="";
$matches=array();

$pat='!([a-z0-9_]+)\.'.preg_quote($mydomain).'$!i';
if (preg_match($pat,$_SERVER['HTTP_HOST'],$matches)) $subdomain=$matches[1];

Too much talk of such a little problem.
Everyone says its dangerous but noone bother to write a solution, as simple as

$mydomain='example.com';
$subdomain="";
$matches=array();

$pat='!([a-z0-9_]+)\.'.preg_quote($mydomain).'$!i';
if (preg_match($pat,$_SERVER['HTTP_HOST'],$matches)) $subdomain=$matches[1];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文