increment counterfile savely
Moderator: General Moderators
increment counterfile savely
-
Hi,
I have coded a text-file based counter.
I can't manage to retrieve, increment and save back savely the new count. For some reason the count is zero again from time to time due to some filehandling problems.
I tried flock several times without success. File is not locked or whatever, I even can write on locked files. This function didn't ever work on all servers (linux/unix/bsd/gentoo) I tried.
So how to savely first read, then increment and write back the data
- additonally ensuring that several simultaneously calls of the counter-script won't screw up the count??
thx in advance,
djot
-
Hi,
I have coded a text-file based counter.
I can't manage to retrieve, increment and save back savely the new count. For some reason the count is zero again from time to time due to some filehandling problems.
I tried flock several times without success. File is not locked or whatever, I even can write on locked files. This function didn't ever work on all servers (linux/unix/bsd/gentoo) I tried.
So how to savely first read, then increment and write back the data
- additonally ensuring that several simultaneously calls of the counter-script won't screw up the count??
thx in advance,
djot
-
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
with [php_man]file_get_contents[/php_man], [php_man]fopen[/php_man], [php_man]is_file[/php_man], and [php_man]unlink[/php_man]
like this:
get the counter file's content, do the math.
fopen the counter file, check if an atomic file exists.
If the atomic file exists, close the file.
If the atomic file doesn't exist, create the atomic file, and overwrite the data in the counter file.
unlink the atomic file so another update can be done.
that is, if there isn't a problem in your flock..
like this:
get the counter file's content, do the math.
fopen the counter file, check if an atomic file exists.
If the atomic file exists, close the file.
If the atomic file doesn't exist, create the atomic file, and overwrite the data in the counter file.
unlink the atomic file so another update can be done.
that is, if there isn't a problem in your flock..
code
-
Hi,
this is the code snipplet:
I didn't get what "atomic file" is. Like having an extra file (e.g. filelock.tmp) that is set when writing and afterwards deleted when counter-file (e.g. counter.txt) is written?
I also tried first writing a temp file, then deleting the old counter-file and then renaming the temp-file to the counter-file-name again. Without success.
djot
-
Hi,
this is the code snipplet:
Code: Select all
if ($fp = @fopen($file, "r+"))
{
@flock($fp, LOCK_SH);
$count = fread($fp, filesize($file));
@flock($fp, LOCK_UN);
fclose($fp);
}
$count++;
if ($fp = @fopen($file, "w+"))
{
@flock($fp, LOCK_EX);
@fputs($fp, $count);
@flock($fp, LOCK_UN);
@fclose($fp);
}I also tried first writing a temp file, then deleting the old counter-file and then renaming the temp-file to the counter-file-name again. Without success.
djot
-
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
an atomic file is just a file that only exists to mark that the real file is in use, so that other scripts don't try to access it.. it can still fail, sorta.. In that if the site is hit with a LOT of hits in very rapid succession, the time it takes to see if "x" is a file, another script could be at the same instance, and also see that the file doesn't exist.. al this really loses is a counter increment..
-
Hi,
so I did know what atomic file was
the problem occurs even with very low hitrates (100 a day) what is really annoying and what i thought rather impossible even without using (f)locking.
I studied many other counters/forums/text-based-cms codes for their writing protection or their file handling functions. But most even don't use flock nor any other protection. Don't really understand why and how they don't face the mis-overwriting problem.
djot
-
Hi,
so I did know what atomic file was
the problem occurs even with very low hitrates (100 a day) what is really annoying and what i thought rather impossible even without using (f)locking.
I studied many other counters/forums/text-based-cms codes for their writing protection or their file handling functions. But most even don't use flock nor any other protection. Don't really understand why and how they don't face the mis-overwriting problem.
djot
-
Well personally, for a simple counter I prefer text files over a DB.
The PHP parser might run into problems when incrementing a string variable, so make sure to trim the string being returned from the file, and then convert it to an integer, and increment the number.
Example of variable conversion:
The PHP parser might run into problems when incrementing a string variable, so make sure to trim the string being returned from the file, and then convert it to an integer, and increment the number.
Example of variable conversion:
Code: Select all
$string = "0 ";
$int = (int) trim ($string);
$count = $int++;