increment counterfile savely

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

increment counterfile savely

Post 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
-
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

maybe you should show us your original code using flock. Perhaps there was something wrong with the code you created
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

code

Post 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
-
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post 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
-
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it'd be a lot easier to store the counter in a database.
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post 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
-
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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++;
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post 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
-
Post Reply