如何使用 PHP 获取第一个子域? [英] How to get the first subdomain with PHP?

查看:41
本文介绍了如何使用 PHP 获取第一个子域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 dev.example.com 的静态域,带有像这样的通配符子域 *.dev.example.com.

I have a static domain of dev.example.com with wildcard subdomains like so *.dev.example.com.

我需要检测当前通配符子域的名称.因此,如果我正在浏览 sub.dev.example.com,我如何获得sub"?

I need to detect the name of the current wildcard subdomain. So if I'm browsing sub.dev.example.com how do I get "sub"?

$env_domain = dev.example.com;
$subdomain = array_shift( explode( '.', $_SERVER['HTTP_HOST'] ) ) .'.'. $env_domain;
echo $subdomain;

目前,这将返回 dev.我需要它返回 sub.

Currently, this returns dev. I need it to return sub.

我认为这里的最佳做法是返回最低级别的域(第一个子域).

I'm thinking the best practice here would be to return the most low-level domain (the first subdomain).

请注意,我不是在解析 URL,而是在解析给定的域.

Note that I'm not parsing a URL, but a given domain.

推荐答案

$domain = 'sub.dev.example.com';
$tmp = explode('.', $domain);
$subdomain = current($tmp);
print($subdomain);     // prints "sub"

这篇关于如何使用 PHP 获取第一个子域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆