Good morning, friends. I've struggled and googled and I can't seem to find an answer. If it's been answered on here already and I missed it, please feel free to direct me to what I missed.
I have the following bit of PHP code. It works fine when I run it on my Mac (and the non-Windows machines of some friends), but when I run it on Windows (where it's going to live here at work), it writes each line to the text file twice. PHP here at work is 5.3.6 running on IIS 6.
Any help is greatly appreciated.
<?php
$fltSupervision = trim(htmlentities($_POST["txtSupervision"]));
$fltDrugTestUA = trim(htmlentities($_POST["txtDrugTestUA"]));
$fltAlcoholSensor = trim(htmlentities($_POST["txtAlcoholSensor"]));
$fltTotal = trim(htmlentities($_POST["txtTotal"]));
$fltSurcharge = trim(htmlentities($_POST["txtSurcharge"]));
$strTransaction = $fltSupervision . "\t" . $fltDrugTestUA . "\t" . $fltAlcoholSensor . "\t" . $fltSurcharge . "\t" . $fltTotal . PHP_EOL;
echo $strTransaction;
$file = 'fees.txt';
file_put_contents($file, $strTransaction, FILE_APPEND);
?>
Jeremy
It probably has something to do with a missing favicon.ico.
Try changing .htaccess to this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [NC,L]
And try creating a favicon.ico file by creating an empty .txt file renaming it to favicon.ico.
Many browsers look for a favicon.ico file and if that file doesn't exist and your .htaccess file is setup to direct to your script, the script will be executed twice and the text will be added twice to fees.txt.
This can only possible if you are refreshing your page. Your current code will append your contents in fees.txt file.
It looks like the PHP_EOL may be the thing that is throwing your text editor. If the PHP_EOL is defined as " " this could produce a space between lines in your text file.
Try and set your own end of line character to just " " and see if this fixes it.
$strTransaction = $fltSupervision . "\t" . $fltDrugTestUA . "\t" . $fltAlcoholSensor . "\t" . $fltSurcharge . "\t" . $fltTotal . "
";
I tested your code on my server and it checks out. There may something else causing this (if that is the case), maybe your form itself. If you reload the page twice, then yes it will save it twice, because of FILE_APPEND
Did you maybe hit the submit button twice by accident maybe? Just checking out the possibilities. If you don't want it to append, then take out , FILE_APPEND
The answer ends up being pretty weird. After lots of testing, I discovered that it only happens when I use a Windows machine at work to load the script from a Windows server at work. If the client machine is not on our corporate network, it behaves as expected. Since weird network behavior is probably far outside the scope of the original post, let's take that as the answer. I began to understand this when I followed Casper's advice and ran the script from command line on my Windows server.
Thanks, everyone for giving me ideas and helping me learn a little more. Have a great day.