File Locking
| From: | Brian V Bonini | Date: | Tue, 29 Apr 2003 23:05:04 +0000 |
| Subject: | File Locking | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-145711@lists.php.net to get a copy of this message | ||
Ok, obviously I'm not understanding this whole file locking thing.
I have this:
<?php
$counterFile = "./counter.txt";
function displayCounter($counterFile) {
global $counted;
$fp = fopen($counterFile, 'rw');
flock ($fp, LOCK_SH);
$num = fgets($fp,7);
flock($fp, LOCK_UN);
if (!$counted) {
flock($fp, LOCK_EX);
$num += 1;
unlink("$counterFile");
exec("echo $num > $counterFile");
flock($fp, LOCK_UN);
}
print "Visitor #$num";
}
if (!file_exists($counterFile)) {
exec("echo 1 > $counterFile");
}
displayCounter($counterFile);
/*
following is at top of the page to not have the same visitor counted
over and over
if (!$counted) { setcookie("counted",1,time()+600); }
*/
?>
But, I have concurrency issues with it and the counter.txt file gets
reset to zero when it happens so I tried adding in the flock functions
but it's not working, I'm sure I'm just using them wrong but.... Help,
anyone? Thanks!!