Hello forum
I have experienced some strange problems,
and it would be great if anyone had some thoughts to clear my mind.
Inside a massive script there are some functions that acts like hitcounters.
They read an integer-number from a file, increases this number with 1,
and writes the new number to the file.
This is the code for the job:
// read the number from a file and increase by 1
$cont=@fopen($somefile,'r'); $incr=fgets($cont); fclose($cont); $incr++;
// writes the number
$cont=@fopen($somefile,'w'); fwrite($cont,$incr); fclose($cont);
...............................................
Everything works as intended untill all of a sudden one of the $somefiles gets
overwritten with an empty character. I have tested variants of the function
above, as example:
if (!$hm_333 = fopen($somefile, 'w')){die;}if(!fwrite($hm_333, $lg13)){die;}
I also made one that checked it was a numeric value that was read/written,
and otherwise died, but no matter what I do, eventually the file is overwritten
with an empty character, and filesize=0. I hope you get the picture.
It seems to me that the problem are too many connections at the same time,
but this is happening with regular activity on a webserver, not some agressive benchmarking.
Does anyone have a foolproof function for increasing a number from/to a file?
It is really frustrating to not be able to depend on a single number being counted correctly,
without having to make some SQL.connection.
Thanks!
:-)