我以前没有使用$_SERVER['HTTP_REFERER']
过,我不知道如何通过点击 Google 搜索结果中的链接来找到该用户来到我的网站。用户可以来自 google.com、google.co.uk 等......
非常感谢帮助。
$url = $_SERVER['HTTP_REFERER'];
$query = parse_url ($url, PHP_URL_QUERY);
$host = parse_url ($url, PHP_URL_HOST);
if (strstr ($query, 'q=') && strstr ($host, 'google.')) {
// user came from google
else {
// user didnt come from google
}
这是我的解决方案,当页面请求中不存在引荐来源网址时,它不会发出任何警告。
public function doesUserCameFromCrawler() {
$crawlerList=array("google.","yandex.","bing.");
if (isset($_SERVER['HTTP_REFERER'])) {
$referringPage = parse_url( $_SERVER['HTTP_REFERER'] );
if ( isset( $referringPage['host'] )) {
$referringHost = $referringPage['host'];
foreach ($crawlerList as $crawler) {
if (strpos(strtolower($referringHost),$crawler) !== FALSE) {
return TRUE;
}
}
}
}
return FALSE;
}