One way to avoid deleting the file is to write a copy, then move it.
This is mostly overkill, but hopefully it will illustrate the process.
/ get the current count /
$yourCounterFile = "/path/to/counterFile";
$fp = fopen($yourCounterFile,"r");
while (($line = fgets($fp,10)) != null) {
$newCount = ++$line;
}
fclose($fp);
/ write the new count to a temp file /
$tmpFile = "/tmp/".md5(date('s'))";
$fp = fopen($tmpFile,"w");
fputs($fp,$newCount);
fclose($fp);
/ move and delete the file /
if (file_exists($tmpFile))
rename($tmpFile,$yourCounterFile);
Laguna wrote:
Hey!
I am making a counter. It is called on by an image:
ie <img src="www.mysite.com/counter.php?site=1" width=1 height=1>
When it is called it opens a file, reads it, adds one to it, and writes to it.
However, for some reason, the counter will reset itself, or sometimes delete the file contents.
I think it is because people stop their browsers, but I am not sure. I did flock the file. Can anyone solve my problem!
Thanks!