1

我以前没有使用$_SERVER['HTTP_REFERER']过,我不知道如何通过点击 Google 搜索结果中的链接来找到该用户来到我的网站。用户可以来自 google.com、google.co.uk 等......

非常感谢帮助。

4

2 回答 2

9
$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
}
于 2012-05-16T06:26:34.937 回答
1

这是我的解决方案,当页面请求中不存在引荐来源网址时,它不会发出任何警告。

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;
}
于 2012-08-20T07:48:03.787 回答