[PHP] Counter has gremlins
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
Re: [PHP] Counter has gremlins
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
Re: [PHP] Counter has gremlins
I believe this is a concurrency problem... 2 PHP scripts can run at the same time and the problem is that your first script gets as far as the unlink thus deleting the file. Then the 2nd script is ran (at the same time) and trys to open the file which doesn't exist, therefore it reads a num of 0. Then control returns to script 1 which saves the number num (which is a valid count), and finish... Script 2 continues and saves the num 0, and therefore your counter gets reset. Its all a problem of concurrency and the fact that more than one thing can happen at a time. There are a few solutions. 1) Use MySQL or another database which limits reading/writing to the data so that problems like this do not occur. 2) Use a flocking method which locks the file between the reading and writing so that no other script can interfeare... I'm unsure on how good this method is with PHP, so I suggest the first. Andrew - Original Message - From: Brian V Bonini [EMAIL PROTECTED] To: PHP Lists [EMAIL PROTECTED] Sent: Tuesday, February 18, 2003 12:45 AM Subject: [PHP] Counter has gremlins 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