2011-09-08 69 views

回答

7
if (false !== stripos($_SERVER['HTTP_REFERER'], "www.someexample.com")){ 
    //do stuff 
} 
+1

提出,不區分大小寫,使其多了幾分可靠。 – ceejayoz

+3

HTTP_REFERER = www.someotherexample.com?param=www.someexample.com =) – Fivell

+0

@ceejayoz:是的,謝謝 – genesis

0

如果使用Apache,只需$ _ SERVER [ 'HTTP_REFERER']

2
if(stripos($_server['HTTP_REFERER'], 'someexample.com') !== FALSE) { 
    // The link is from someexample.com (might not have "www" in it) 
} 

注意執行preg_match這也將匹配http://www.andsomeexample.com。如果要防止這種情況,使用parse_url

if(parse_url($_SERVER['HTTP_REFERER'])['host'] == 'someexample.com'){ 
    // You're good to go... 
} 
+0

如果(由於某種原因)字符串在引用字段的開始 - stripos將返回0,這將計算爲false失敗。 –

+0

HTTP_REFERER = www.someotherexample.com?param=www.someexample.com =) – Fivell

+0

這會觸發解析錯誤! – genesis

0
echo parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST); 
相關問題