Referrer Log script - don't log certain domains?

Hi. I have this script that I use for logging referrer urls.

<?php
$file = "refers.txt";
$log_ip = 0;
$url = parse_url($_SERVER['HTTP_REFERER']);
$referer = (!isset($_SERVER['HTTP_REFERER']) || $_SERVER['HTTP_REFERER'] == '') ? 'an unknown url/direct access (typing in URL)' : $_SERVER['HTTP_REFERER'];
$ip = ($log_ip == 1) ? $_SERVER['REMOTE_ADDR'] : false;
$time = date('d F Y');
$user_text  = ($log_ip == 1) ? "On {$time} {$ip}" : "On {$time} a user";
$refer_text = "{$referer}";

if ($url['host'] !== $_SERVER['HTTP_HOST'] && $referer != 'an unknown url/direct access (typing in URL)')
{

$fp = fopen($file, 'a');
fwrite($fp, "{$refer_text}\
");
fclose($fp);

}

?>

Is it possible to not log referrers from a particular url domain? So for example, if a user came from a site called www.testsite.com it wouldn’t log that referrer visit in the text file?

Thanks.

Hi,

You could prevent certain domains from being logged by keeping an array of the domains and checking the referrer url against the list before writing to the log.


$file = "refers.txt";
$log_ip = FALSE;
$blacklist = array(
    'example.com',
    'foobar.co.uk'
);

// filter_input returns the value if set, or NULL if not.
$ip = filter_input(INPUT_SERVER, 'REMOTE_ADDR');
$referrer = filter_input(INPUT_SERVER, 'HTTP_REFERER');

$url = ($referrer) ? parse_url($referrer) : false;

$time = date('d F Y');
$user_text  = ($log_ip) ? "On {$time} {$ip}" : "On {$time} a user";
$refer_text = "{$referer}";

// if we have a valid referrer url and the host is not on our blacklist
if (isset($url['host']) && !array_key_exists($url['host'], $blacklist))
{
    // fopen, fwrite and fclose can be replaced by this one function
    file_put_contents($file, "{$refer_text}\
", FILE_APPEND);
}

I’ve suggested a few other changes to the code, which are commented so you can see what they do. I notice that $user_text doesn’t actually get used anywhere - is that supposed to be part of the message which is written to the log?

$user_text was just part of the original script but I only wanted it to log the referrer so edited it a bit.

I just tried using your code which I edited a bit too, but it doesn’t seem to log the referrer, it just leaves a blank space where the referrer text should go. This is the code I’m using.

<?php
$file = "refers.txt";
$blacklist = array('pegaminidesign.blogspot.it','foobar.co.uk');
$referrer = filter_input(INPUT_SERVER, 'HTTP_REFERER');
$url = ($referrer) ? parse_url($referrer) : false;
$refer_text = "{$referer}";

if (isset($url['host']) && !array_key_exists($url['host'], $blacklist))
{
    file_put_contents($file, "{$refer_text}\
", FILE_APPEND);
}
?>

Oops, there’s a small typo here:

$refer_text = "{$referer}";

should be:

$refer_text = "{$referrer}";

It’s logging referrers now, but it still seems to log all referrers including those in the ignore list. Tested it on a couple of sites.

Oops, sorry, I messed up… the function should be in_array, not array_key_exists, so the code should look like this:


if (isset($url['host']) && !in_array($url['host'], $blacklist))
{
    file_put_contents($file, "{$refer_text}\
", FILE_APPEND);
}

Really sorry about that, I don’t know what I was thinking!

Ha no problem, you’re the only person that’s been kind enough to help me so thanks for taking the time to do this and it’s working great now thanks! :slight_smile: