note 16187 deleted from function.fwrite by jimw

From: Date: Fri, 19 Oct 2001 18:04:41 +0000
Subject: note 16187 deleted from function.fwrite by jimw
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-19161@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);

« previous php.notes (#19161) next »