SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2010
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Referrer Log script - don't log certain domains?

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

    Code:
    <?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}\n");
    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.

  2. #2
    SitePoint Mentor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,256
    Mentioned
    32 Post(s)
    Tagged
    5 Thread(s)
    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.

    PHP Code:
    $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}\n"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?

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2010
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $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.

    Code:
    <?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}\n", FILE_APPEND); 
    }
    ?>

  4. #4
    SitePoint Mentor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,256
    Mentioned
    32 Post(s)
    Tagged
    5 Thread(s)
    Oops, there's a small typo here:
    PHP Code:
    $refer_text "{$referer}"
    should be:
    PHP Code:
    $refer_text "{$referrer}"

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2010
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  6. #6
    SitePoint Mentor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,256
    Mentioned
    32 Post(s)
    Tagged
    5 Thread(s)
    Oops, sorry, I messed up.. the function should be in_array, not array_key_exists, so the code should look like this:
    PHP Code:
    if (isset($url['host']) && !in_array($url['host'], $blacklist)) 

        
    file_put_contents($file"{$refer_text}\n"FILE_APPEND); 

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

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2010
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •