2011-09-08 37 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); 
相关问题