Simply put the code in your sites functions.php and call it in page_head.php!
check_referer()
check_referer("./difname")
function is_ipv4($s)
{
$n = "([0-9]|1?\d\d|2[0-4]\d|25[0-5])";
return preg_match("#^{$n}\.{$n}\.{$n}\.{$n}$#", $s);
}
function check_referer($db_referer = "./referer.txt")
{
if ( !preg_match("#http://(.*?)/(.*)#", $_SERVER["HTTP_REFERER"], $m) )
{
return false;
}
if ( !is_ipv4($m[1]) && is_numeric(strpos($m[1], '.')) && substr_count($m[1], '.') > 1 )
{
$m[1] = strrev($m[1]);
$m[1] = substr($m[1], 0, strpos($m[1], '.', strpos($m[1], '.') + 1));
$m[1] = strrev($m[1]);
}
if ( $m[1] == $_SERVER["HTTP_HOST"] )
{
return false;
}
if ( file_exists($db_referer) )
{
$referer = unserialize(file_get_contents($db_referer));
}
else
{
$referer = array();
}
if ( !isset($referer[$m[1]]) )
{
$referer[$m[1]] = 0;
}
$referer[$m[1]]++;
file_put_contents($db_referer, serialize($referer));
return true;
}