check_referer()

By sean on Sep 17, 2006

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;
}

Comments

Sign in to comment.
Hawkee   -  Sep 18, 2006

What exactly does this do? Might be nice to have an explanation of the purpose in the description.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.