Here are 2 scripts which illustrate my problem:
<?php
$filename = "testfile.txt";
$data = file_get_contents($filename);
if ($data == 1)
{
echo "one stored in file - access denied";
exit();
}
else
{
//store 1 in file
$data = 1;
$fp= fopen($filename,'w');
fwrite($fp, $data);
fclose($fp);
//here I call test2.php via a header command
exit();
}
?>
<?php
//store 0 in file
$data = 0;
$filename = "testfile.txt";
$fp= fopen($filename,'w');
fwrite($fp, $data);
fclose($fp);
echo "zero stored in file";
exit();
?>
test1 reads a file, and either denies access (if there is a 1 stored in it) or else calls test2 via a header command. test2 stores a zero in the file. I would have thought that I could run test1 over and over again, as it will always call test2 which always stores a zero.
However, I can't. test1 always calls test2 the first time I run it, but on either the 2nd or 3rd occasion test1 always denies access, meaning that the previous run of test 2 has failed to write to the file even though it has run without an error.
Thereafter, however many times I run test2, test1 always denies access. I've looked at the raw file on the server and it confirms that in this situation test2 isn't actually storing the zero.
If I shut down the browser and reload it, then run test2, it works properly and test1 works again. I am sure this is significant.
All my coding efforts seem to run into a version of this problem – very depressing.
What is wrong and what do I have to do to correct it?