...

View Full Version : reset counter



haithomy
11-09-2011, 07:33 PM
I have this 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?

Fou-Lu
11-09-2011, 07:48 PM
24 hours from what? Or do you mean a specific time?

haithomy
11-09-2011, 07:58 PM
auto reset every midnight

mlseim
11-09-2011, 08:06 PM
Who's midnight?
Your servers timezone?
Your timezone?
My timezone?

Fou-Lu
11-09-2011, 08:20 PM
Just need to add a way to track the date. Something simple would be fine.


$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($fh, 0, SEEK_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.


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.

haithomy
11-25-2011, 03:41 PM
Hi I found the solution to save the hits in DB:



$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

Fou-Lu
11-25-2011, 04:11 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum