這是我會怎麼做。它會解析給定的URL(如果有),然後刪除所有不需要的信息,例如Top-Level-Domain或第三級或更低級別的域,所以我們只剩下第二級域(google)。
function isRequestFromGoogle() {
if (!empty($_SERVER['HTTP_REFERER'])) {
$host = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);
if (!$host) {
return false; // no host found
}
// remove the TLD, like .com, .de etc.
$hostWithoutTld = mb_substr($_SERVER['HTTP_REFERER'], 0, mb_strrpos($_SERVER['HTTP_REFERER'], '.'));
// get only the second level domain name
// e.g. from news.google.de we already removed .de and now we remove news.
$domainName = mb_substr($hostWithoutTld, mb_strrpos($hostWithoutTld, '.') + 1);
if (mb_strtolower($domainName) == 'google') {
return true;
} else {
return false;
}
}
}
在代碼的頂部使用'session_start();'。 – HamZa 2013-03-26 08:29:01
關鍵字是** referer **。諷刺的是,谷歌應該幫助。 – deceze 2013-03-26 08:30:14
http://stackoverflow.com/questions/700672/how-do-you-detect-if-your-website-visitor-came-from-a-google-search-result – 2013-03-26 08:33:37