Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-09-2011, 07:33 PM   PM User | #1
haithomy
New to the CF scene

 
Join Date: Apr 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
haithomy is an unknown quantity at this point
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?
haithomy is offline   Reply With Quote
Old 11-09-2011, 07:48 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,449 Times in 2,418 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
24 hours from what? Or do you mean a specific time?
Fou-Lu is offline   Reply With Quote
Old 11-09-2011, 07:58 PM   PM User | #3
haithomy
New to the CF scene

 
Join Date: Apr 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
haithomy is an unknown quantity at this point
auto reset every midnight
haithomy is offline   Reply With Quote
Old 11-09-2011, 08:06 PM   PM User | #4
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
Who's midnight?
Your servers timezone?
Your timezone?
My timezone?
mlseim is offline   Reply With Quote
Old 11-09-2011, 08:20 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,449 Times in 2,418 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 11-25-2011, 03:41 PM   PM User | #6
haithomy
New to the CF scene

 
Join Date: Apr 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
haithomy is an unknown quantity at this point
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
haithomy is offline   Reply With Quote
Old 11-25-2011, 04:11 PM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,449 Times in 2,418 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:43 AM.


Advertisement
Log in to turn off these ads.