检测用户是否在子域中,然后使用PHP添加变量?

Ada*_*hur 4 php variables subdomain detection

我想知道如何创建一个PHP IF语句来检测用户是否正在加载某个URL.在我的情况下它是:

www.design.mywebsite.com
Run Code Online (Sandbox Code Playgroud)

IF语句需要覆盖整个子域,EG也必须检测到URL:

www.design.mywebsite.com/filename.php
Run Code Online (Sandbox Code Playgroud)

一旦建立了IF语句,我想添加一个PHP变量.所以它可能会帮助别人,让我们称之为:

$myvariable = "Hey there! You're on my subdomain";
Run Code Online (Sandbox Code Playgroud)

总结 一下:最终的代码应该是这样的......

<?php
  if //Code to detect subdomain// {
    $myvariable = "Hey there! You're on my subdomain";
  }
?>
Run Code Online (Sandbox Code Playgroud)

Bab*_*aba 11

一个简单的解决方案

$host = "baba.stackoverflow.com"; // Your Sub domain

if ($_SERVER['HTTP_HOST'] == $host) {
    echo "Hello baba Sub Domain";
} else {
    echo "not baba domain";
}
Run Code Online (Sandbox Code Playgroud)

如果你有的话,也可以使用 parse_url http://php.net/manual/en/function.parse-url.phpURL

$url = "http://baba.stackoverflow.com/questions/10171866/detect-if-a-user-is-on-a-subdomain-and-then-add-a-variable-using-php";
$info = parse_url($url);
if ($info['host'] == $host) {
    echo "Hello baba Sub Domain";
} else {
    echo "not baba domain";
}
Run Code Online (Sandbox Code Playgroud)

请更换$url$_GET;

  • 出色的答案 - 不可能要求更好.工作就像一个魅力.非常感谢巴巴. (2认同)