0

如果有人从 google 访问我的网站,我如何使用 php 找到?

<?php
  if (isset($_COOKIE['source'])) {
      $arr = explode("=",$_COOKIE['source']);
      $_SESSION['source'] = $arr[1];
      unset($_COOKIE['source']);
  }
?>

这就是我获取来源的方式,以了解访问者在我的网站之前在哪里,并且我想设置$_SESSION['source']="google"他是否在 Google 上搜索以找到我的页面。

4

3 回答 3

1

使用:$_SERVER['HTTP_REFERER']

if(strpos($_SERVER['HTTP_REFERER'], 'google'))
echo 'comes from google';
于 2013-03-26T08:32:47.287 回答
0

尝试检查 $_SERVER['HTTP_REFERER']。此全局变量应包含引荐来源网址。

于 2013-03-26T08:30:55.390 回答
0

我就是这样做的。它会解析给定的 URL(如果有),然后删除所有不需要的信息,例如顶级域或三级或更低级域,因此我们只剩下二级域(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;
        }
    }
}
于 2013-03-26T08:43:50.520 回答