Trying to edit this simple referral script

I don’t know php so I went searching for a php script that will just log all outside referral links into a text file and found the following:

<?php
/*
* script by spyka webmaster - www.spyka.net
* version: 1.0.0
* copyright (c) 2008 spyka Web Group
* license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

// file where referrals are stored, change if you wish
$file = "refers.txt";

// if set to 1 IPs will be logged and associated with the URL they were referred by
$log_ip = 0;

//////////////////// NO NEED TO EDIT BELOW ////////////////////

$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 = "{$user_text} was referred by {$referer}";
$fp = fopen($file, 'a');
fwrite($fp, "{$refer_text}\
");
fclose($fp);

?>

This logs outside referrers but it also logs visits when you visit the site directly without a referral and even logs the site itself when clicking links through on to another page on the site.

How do I change it so it only logs outside referrals, nothing else?

Try this:


<?php

$url = parse_url($_SERVER['HTTP_REFERER']);


if ($url['host'] !== $_SERVER['HTTP_HOST'])
{
	// code to be executed
}

Thanks, that stopped the site from showing as a referrer, though it still logs every time somebody directly visits the site with “by an unknown url/direct access (typing in URL)”. Here’s the current code I’m using.

<?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 = "{$user_text} was referred by {$referer}";

if ($url['host'] !== $_SERVER['HTTP_HOST'])
{

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

}

?>

That’s… what your code should do? If they directly visit the site from no outside source, Referer = NULL.

Using HTTP_REFERER is… unreliable at best, some browsers dont send referer information at all, and it’s easily manipulated (because it is in effect a user-sent variable)

Ideally I only want to log referrers, not when somebody visits the site directly. I’m not too fussed about it being unreliable since it’s just a personal site.

so your problem is that… ($url[‘host’] !== $_SERVER[‘HTTP_HOST’]) is returning true.

Why are we not using SERVER_NAME here instead of HTTP_HOST here?

Anyway, throw in an “&& $referer != ‘an unknown url/direct access (typing in URL)’” on your if there should take care of that issue.

That works, thanks!

It might be worth reading the following discussion: