如何使用$ _SERVER ['HTTP_REFERER']查找该用户来自谷歌?

时间:2012-05-16 06:18:58

标签: php search

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

非常感谢。

2 个答案:

答案 0 :(得分:8)

$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
}

答案 1 :(得分:0)

我的解决方案在页面请求中不存在引荐来源时不会发出任何警告。

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;
}