help with unique visitor count

Discussion in 'PHP' started by wckd, Feb 3, 2008.

  1. #1
    I was wondering if there was a way to have this counter clear after 24 hours.
    I want to put 2 counters on my site. 1 being an all time unique visitor counter (which this will do) and another to count unique visitors for the day.
    
    <?php
    $filename = "hits.txt";
    
    $file = file($filename);
    $file = array_unique($file);
    $hits = count($file);
    echo $hits;
    
    $fd = fopen ($filename , "r");
    $fstring = fread ($fd , filesize ($filename));
    fclose($fd);
    $fd = fopen ($filename , "w");
    $fcounted = $fstring."\n".getenv("REMOTE_ADDR");
    $fout= fwrite ($fd , $fcounted );
    fclose($fd);
    ?>
    Code (markup):

     
    wckd, Feb 3, 2008 IP
  2. aredhelrim

    aredhelrim Peon

    Messages:
    7
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    write a php script for remove hits.txt ( php.net/unlink ) and this script must create hits.txt after delete. lastly add cronjob to 00:00 from your control panel.
     
    aredhelrim, Feb 3, 2008 IP
  3. wckd

    wckd Active Member

    Messages:
    711
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    80
    #3
    is there anyway to just delete the hits.txt content?
     
    wckd, Feb 3, 2008 IP
  4. ToddMicheau

    ToddMicheau Active Member

    Messages:
    183
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #4
    One suggestion might be to check the file creation time before writing to the file, if that is one or more days ahead of the servers current time then delete the record file and make a new one. This would only work on a Windows server though, seeing as *nix does not store creation timestamps.

    To check the creation time on a file check out:
    http://us.php.net/manual/en/function.filectime.php

    (Again if your using a *nix server this function will return the last time it was changed, if your on windows it will get the creation time.)

    You could also set a cron job to do it, if your host allows it.
     
    ToddMicheau, Feb 3, 2008 IP