note 16187 added to function.fwrite
| From: | larstr at nospam dot colargol dot tihlde dot org | Date: | Fri, 19 Oct 2001 10:38:15 +0000 |
| Subject: | note 16187 added to function.fwrite | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-19151@lists.php.net to get a copy of this message | ||
I've noticed that several of the counters contributed here are not fail safe. If they get some
load on such a page, the counter will fail. Several here are doing something similar to this:
1. openi the counter file to read it
2. read the value and increments it
3. close the file
4. open the file again for writing
5. write the value
6. close the file
Imagine that between point 3 and 4 there might be another user gaining read access the the file, and
we have a counter chaos ;)
The solution is simple. Keep the file open as long as you're working on it:
$counter = "counter";
$fd = fopen($counter, "r+");
$num = fread($fd, filesize( $counter ));
rewind($fd);
$users = $num + 1;
fwrite($fd, $users);
fclose($fd);
--
http://www.php.net/manual/en/function.fwrite.php
http://master.php.net/manage/user-notes.php?action=edit+16187
http://master.php.net/manage/user-notes.php?action=delete+16187
http://master.php.net/manage/user-notes.php?action=reject+16187