PHP:搜索字符串中的重合处

发布于 2024-09-13 09:15:07 字数 506 浏览 4 评论 0原文

我想过滤访问者从搜索引擎访问我的网站的 HTTP_REFERER。我想忽略为来自搜索引擎的访问者存储 HTTP_REFERER 信息。您能帮忙编写一下 PHP 脚本吗?

我有这个,但不正确的脚本:

<?
$exp_list = array('google', 'yahoo');

// exapmple of one HTTP_REFERER link from the Goggle search engine
$link = 'http://www.google.com/search?hl=ru&source=hp&q=bigazart&aq=f&aqi=&aql=&oq=&gs_rfai=';

for ($j = 0; $j < sizeof($exp_list); $j++){

if(!eregi($exp_list[$j], $link)){

// storing link to mysql...

break;

}

}
?>

I would like to filter HTTP_REFERER where visitors come to my site from search engines. I want to ignore storing HTTP_REFERER information for the visitors came from the search engines. Could you please help with the PHP script?

I have this, but not correct script:

<?
$exp_list = array('google', 'yahoo');

// exapmple of one HTTP_REFERER link from the Goggle search engine
$link = 'http://www.google.com/search?hl=ru&source=hp&q=bigazart&aq=f&aqi=&aql=&oq=&gs_rfai=';

for ($j = 0; $j < sizeof($exp_list); $j++){

if(!eregi($exp_list[$j], $link)){

// storing link to mysql...

break;

}

}
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜宝宝 2024-09-20 09:15:07

尝试这样的事情:

if (isset($_SERVER['HTTP_REFERER'])) {
    $host = strtolower(parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST));
    $exp_list = array('google', 'yahoo');
    $pattern = '/^(?:www\.)?(?:'.implode('|', array_map('preg_quote', $exp_list)).')\./'
    if (preg_match($pattern, $host)) {
        // match found
    }
}

重要的事情:

  • 检查 $_SERVER['HTTP_REFERER'] 是否存在
  • 使用 parse_url 从 URL 获取主机并仅在那里搜索
  • 测试术语是否被点包围

但这仍然会错误地识别像 www.google.example 这样的主机。 com。因此您可能还想指定顶级/二级域名。

Try something like this:

if (isset($_SERVER['HTTP_REFERER'])) {
    $host = strtolower(parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST));
    $exp_list = array('google', 'yahoo');
    $pattern = '/^(?:www\.)?(?:'.implode('|', array_map('preg_quote', $exp_list)).')\./'
    if (preg_match($pattern, $host)) {
        // match found
    }
}

The important things:

  • Check whether $_SERVER['HTTP_REFERER'] exists or not
  • Use parse_url to get the host from the URL to only search there
  • Test if the terms are surrounded by dots

But this will still falsely identify a host like www.google.example.com. So you might also want to specify the top/second level domain names.

苦妄 2024-09-20 09:15:07

您应该能够自定义以下模式以匹配更多域。

<?php

$ignore_hosts = array(
    '/^www.google.com$/',
    '/^www.yahoo.com$/'
    );

$host = parse_url($_SERVER['HTTP_REFERRER'], PHP_URL_HOST);

$ignore = FALSE;
foreach ($ignore_hosts as $pattern) {
    if (preg_match($pattern, $host) == 0){
        $ignore = TRUE;
        break;
    }
}

if (! $ignore)
    echo "Here you should store the referrer.";

You should be able to customize the patterns below to match more domains.

<?php

$ignore_hosts = array(
    '/^www.google.com$/',
    '/^www.yahoo.com$/'
    );

$host = parse_url($_SERVER['HTTP_REFERRER'], PHP_URL_HOST);

$ignore = FALSE;
foreach ($ignore_hosts as $pattern) {
    if (preg_match($pattern, $host) == 0){
        $ignore = TRUE;
        break;
    }
}

if (! $ignore)
    echo "Here you should store the referrer.";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文