I wrote this counter which keeps track of yes/no for a website and it works decently well, the problem is somehow the files get messed up while writing. For example: it'll go from 126 to 27. The script gets called from an iOS app I wrote so most likely there are multiple connections modifying the file at the same time and I think this is what is causing the problem. I am not really a PHP guy so I was hoping for some insight on what could make the code a little better and handle multiple simultaneous connects.
<?php
$yes_file = 'yes.txt';
$no_file = 'no.txt';
$yes_count = file_get_contents($yes_file);
$no_count = file_get_contents($no_file);
if ($_GET['result'])
{
if( strcmp($_GET['result'], "YES") ) {
$no_count+=1;
file_put_contents($no_file, $no_count);
}
else {
$yes_count+=1;
file_put_contents($yes_file, $yes_count);
}
}
$total = $yes_count + $no_count;
echo "{\"yescount\":" . $yes_count.",";
echo "\"nocount\":" . $no_count.",";
echo "\"total\":" . $total."}";
?>
Thanks!