Page 1 of 1
increment counterfile savely
Posted: Wed Jul 14, 2004 3:56 am
by 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
-
Posted: Wed Jul 14, 2004 4:02 am
by JayBird
maybe you should show us your original code using flock. Perhaps there was something wrong with the code you created
Posted: Wed Jul 14, 2004 4:03 am
by feyd
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..
code
Posted: Wed Jul 14, 2004 4:24 am
by 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 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
-
Posted: Wed Jul 14, 2004 4:31 am
by feyd
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..
Posted: Wed Jul 14, 2004 4:39 am
by 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
-
Posted: Wed Jul 14, 2004 4:42 am
by feyd
it'd be a lot easier to store the counter in a database.
Posted: Wed Jul 14, 2004 4:59 am
by djot
-
Yes thx,
the question was how to write files savely. For sure databases are better, but there is for some reason no way to use a databases in some circumstances!?
djot
-
Posted: Wed Jul 14, 2004 6:38 am
by m3mn0n
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:
Code: Select all
$string = "0 ";
$int = (int) trim ($string);
$count = $int++;
Posted: Wed Jul 14, 2004 7:23 am
by djot
-
Hi,
The problem is not to increment the counter value. That works until the file gets corrupted for some reason that I try to pass by.
djot
-