rolling hit counter script?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • deko

    #1

    rolling hit counter script?

    I use this script to keep count of visits to my site - it's on every page in the
    site.

    <?php
    $file = '/home/post/public_html/viscount';
    if ($counter = @file ($file)) {
    $line = each($counter);
    if (!$counter) {
    $counter = 1;
    } else {
    $counter = $line[value]+1;
    }
    }
    $fp = fopen ($file, "w");
    fwrite ($fp, "$counter") ;
    fclose($fp);
    ?>

    But how to maintain rolling hit counter? For example:

    292 visits in the last 24 hours
    3,841 visits in the last 30 days
    50,389 visits in the last 365 days

    Let's say I want to update these stats every 3 hours - I have a cron job that
    runs at 00:00, 03:00, etc. And I have the following files:

    viscount - number of hits in current interval (3 hours)
    viscount_prev - number of hits in previous interval
    viscount24 - number of hits in last 24 hours
    viscount30 - number of hits in last 30 days
    viscount365 - number of hits in last 365 days

    So, at each interval, a script copies (overwrites) viscount to viscount_prev.
    Then subtract viscount_prev from viscount and add result to viscount24,
    viscount30 and viscount365?

    Has anyone done something like this before? Can you point me to a sample
    script?

    Thanks in advance.


Working...