2012-04-16 21 views
4

我想知道如何創建一個PHP IF語句來檢測用戶是否正在加載/在某個URL上。在我的情況是:檢測用戶是否在子域上,然後使用PHP添加變量?

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"; 
    } 
?> 
+1

這已經被問到:http://stackoverflow.com/questions/5292937/php-function-to-get-the-subdomain-of-a-url – Manuel 2012-04-16 09:49:17

+0

你好dragon112。 我已經看過那個,是的,我知道它 - 但我有點困惑,我怎麼會把它變成一個PHP IF聲明。 – 2012-04-16 09:51:44

+0

感謝評論如此迅速ogur :)。 我知道它需要一個parse_url(),但我不知道該從哪裏去。 – 2012-04-16 09:53:00

回答

11

一個簡單的解決方案是

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

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

您還可以使用parse_urlhttp://php.net/manual/en/function.parse-url.php如果你有URL

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

+2

傑出的答案 - 不能要求更好。像魅力一樣工作。非常感謝你爸爸。 – 2012-04-16 10:33:03

+1

完美的男人...真棒 – Gags 2015-06-24 19:09:12

相關問題