As part of a game I’m developing I would like to be able to track the number of times the game is played. If a player reaches the end of the game they can submit their details and I have the neccessary files in place to track and display this.
However, I would also like to be able to count each and every player who starts a game without neccessarily finishing it.
To this end I have created a simple script that performs a simple counting calculation. I didn’t want to invlove a database for this part of the project as we are anticipating (or at least hoping) to have a fair number of visitors and I don’t want to be making database calls at the start of every game.
So, my counter as it stands is this:
<?php
$num_hits = file_get_contents("playCount.txt");
$num_hits++;
$fp = fopen("playCount.txt", "w");
fwrite($fp, $num_hits);
fclose($fp);
echo $num_hits;
?>
playCount.txt is simply just a text file that contains the number 0 to start with.
This works perfectly and does everything I need it to, but I’ve suddenly realised the implications of opening a file, reading its contents, and then ammending the file, if many users are doing this at the same time. Are there any troublesome effects this could cause? I’m not particularly worried about extreme accuracy of this counter, but would like to know if there are any more serious problems it could cause?