I am trying to make a counter. What I mean by that is a button that uses a XHTMLrequest and just runs this PHP.
My question is why is my counting code changing the value of the text document to the number 1. If I just change the value to for example 24, instead of adding 1 and changing the value to 25, it changes the value to the number 1.
<?php
$fp = false;
// Open file for reading, then writing
while ( ($fp=fopen('clicks.txt','r+'))===false ) {
usleep(250000); // Delay 1/4 second
}
// Obtain lock
while ( !flock($fp, LOCK_EX) ) {
usleep(250000); // Delay 1/4 second
}
// Read Clicks
$clicks = trim(fread($fp,1024));
// Add click
$clicks++;
// Empty file
ftruncate($fp,0);
// Write clicks
fwrite($fp, $clicks);
// Release Lock
flock($fp, LOCK_UN);
// Release handle
fclose($fp);
?>
Replace
$clicks++;with$clicks = $clicks + 1;.