views:

14

answers:

2

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&amp;source=hp&amp;q=bigazart&amp;aq=f&amp;aqi=&amp;aql=&amp;oq=&amp;gs_rfai=';

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

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

// storing link to mysql...

break;

}

}
?>
+1  A: 

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.

Gumbo
A: 

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.";
Lethargy