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


Detect if a user is on a subdomain, and then add a variable using PHP?

我想知道如何创建一个 PHP IF 语句来检测用户是否正在加载/在某个 URL 上。就我而言,它是:

www.design.mywebsite.com

IF语句需要覆盖整个子域,例如还必须检测URL:

www.design.mywebsite.com/filename.php

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

$myvariable = "Hey there! You're on my subdomain";

所以总结一下:最终代码应该看起来像这样...

<?php
  if //Code to detect subdomain// {
    $myvariable = "Hey there! You're on my subdomain";
  }
?>
一个简单的

解决方案是

$host = "baba.stackoverflow.com"; // Your Sub domain
if ($_SERVER['HTTP_HOST'] == $host) {
    echo "Hello baba Sub Domain";
} else {
    echo "not baba domain";
}

如果您有URL,也可以使用parse_url http://php.net/manual/en/function.parse-url.php

$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";
}

请将$url替换为$_GET;