Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: reset counter

  1. #1
    New to the CF scene
    Join Date
    Apr 2004
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    reset counter

    I have this code:

    PHP Code:
    $hits file($_SERVER['DOCUMENT_ROOT']. '/counter.txt');
    $hits implode(''$hits);
    $openfile fopen($_SERVER['DOCUMENT_ROOT']. '/counter.txt''r+');
    $hits++; // Add 1 hit
    fwrite($openfile$hits);
    fclose($openfile); 
    I want the counter to be auto reset every 24 hours?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    24 hours from what? Or do you mean a specific time?

  • #3
    New to the CF scene
    Join Date
    Apr 2004
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    auto reset every midnight

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Who's midnight?
    Your servers timezone?
    Your timezone?
    My timezone?

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Just need to add a way to track the date. Something simple would be fine.
    PHP Code:
    $sPath $_SERVER['DOCUMENT_ROOT']. '/counter.txt';
    if (
    $fh = @fopen($sPath'r+'))
    {
        
    $iLastUpdate 0;
        
    $iCount 0;
        
    fscanf($fh'%d:%d'$iLastUpdate$iCount);
        
    $iNextReset strtotime('00:00:00 +1 day'$iLastUpdate);
        if (
    time() > $iNextResult)
        {
            
    $iLastUpdate strtotime('00:00:00');
            
    $iCount 0;
        }
        ++
    $iCount;
        
    fseek($fh0SEEK_SET);
        
    fwrite($fh$iLastUpdate ':' $iCount);
        
    fclose($fh);

    You could use pack/unpack as well. Try that, its untested, seems to work okay in my head.

    Edit:
    Also, to adjust a timezone, you may use date_default_timezone_set if you want it to change, otherwise it will attempt to find the closest timezone the server is in. You can never base this on a client; to do that you'd need to determine the times data was written, which would be doable in a database.

  • #6
    New to the CF scene
    Join Date
    Apr 2004
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi I found the solution to save the hits in DB:

    PHP Code:
    $hits file($_SERVER['DOCUMENT_ROOT']. '/counter.txt'); 
    $hits implode(''$hits); 
    $last_day mysql_fetch_array(mysql_query("SELECT DAY(date_counter) AS last_day FROM counter ORDER BY date_counter DESC LIMIT 0, 1"));
    if (
    $last_day['last_day'] <> date('d')) {
      
    mysql_query("INSERT INTO counter (hits) VALUES ('" .$hits"')");
      
    $hits 0;
    }
    $openfile fopen($_SERVER['DOCUMENT_ROOT']. '/counter.txt''r+'); 
    $hits++; // Add 1 hit 
    fwrite($openfile$hits); 
    fclose($openfile); 
    My problem now is the $hits not reset to zero and the counter still counted

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You are combining both filesystem and database. Is there a reason for you doing this?
    If you are using a database, you can simply record the date of a hit. SQL can then issue a COUNT for the date with a GROUP BY and a WHERE clause to determine the hits for a day. You don't even need to add a counter if you don't want, just an insert would work fine. Then a simple SELECT count(date_counter) AS count FROM counter WHERE date_counter = CURRENT_DATE() for example. You can use group by if you want to list multiple dates.
    I'd prune them out regularly though. Whether you choose to use a single insert per hit (which will grow fast), or execute an UPDATE instead, chances are you don't need to keep everything for an extended period. If you have no intention of tracking unique entries, then an UPDATE may make more sense. If you want to track unique, then a single entry is the way to go. If its for esthetics only, I'd make it a MEMORY table type as well. It'll disappear on a MySQL restart, but its faster for accessing.


  •  

    Posting Permissions

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