You could be having problems with multiple users, if two visitors come at the same time file locking problems come into play, one script could unlink the file at the same time another script tries to open it resulting in an empty file.
A database would be a better way to do this, but assuming you want to stick with a text based counter instead of using rw and incrementing the count you might consider opening the file in append mode and writing a character to the file, then when you want to find out how many people have visited count the lines in the file. Using file based operations where you may have more than one user operating on one file at the same time can get tricky so like I said consider a database. Jason Mon, 2003-02-17 at 17:45, Brian V Bonini wrote: > I have this basic counter: > > <?php > $counterFile = "./counter.txt"; > function displayCounter($counterFile) { > global $counted; > $fp = fopen($counterFile, 'rw'); > $num = fgets($fp,7); > if (!$counted) { > $num += 1; > unlink("$counterFile"); > exec("echo $num > $counterFile"); > } > print "Visitor #$num"; > } > if (!file_exists($counterFile)) { > exec("echo 1 > $counterFile"); > } > displayCounter($counterFile); > > ?> > > Works like a charm but every so often for no apparent reason it resets > to 0. > > Anyone see anything wrong here to cause that? > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php