我尝试了 $_SERVER['HTTP_HOST']
和 $_SERVER['SERVER_NAME']
,但它们都返回 sub.domain.com,我希望它检查 URL 是 sub.domain.com 还是 just domain.com。
脚本在 sub.domain.com 上运行,它们都返回相同的数据。
$_SERVER['HTTP_HOST']
返回:sub.domain.com
$_SERVER['SERVER_NAME']
返回:sub.domain.com
我只想获取 url 的 domain.com 部分。由于脚本可以在任何子域名上运行,我不确定如何执行,因为上述两个变量返回相同的信息。
最佳答案
我会检查 HTTP_HOST。通常两者相同,但有时 SERVER_NAME 设置不同。如有疑问,请同时检查它们。
'HTTP_HOST'
Contents of the Host: header from the current request, if there is one.
http://php.net/manual/en/reserved.variables.server.php
至于如何查看,可以对主域名进行字符串替换,看结果字符串是否为空。剩下的就是子域。
到目前为止,您正在寻找一些微不足道的字符串操作,这里是最后两部分:
$yourhost = implode('.', array_slice(explode('.', $_SERVER['HTTP_HOST']), -2));
关于php - 如何检查 domain.com 或 sub.domain.com?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10824556/