Hi,
I'm writing tracking information for my website to a file using PHP. 99% of the time it works, but occasionally I get an odd entry, where it looks like fwrite() is called more than once simultaneously, so that two entries get blended into one. Since the lines are very long, I put an example of several correct entries and an incorrect one at:
http://www.sfu.ca/~kganapat/example.html
As you can see, it looks on the 3rd line, each fwrite() is called twice. The PHP script is called via javascript (hence the $_GET's). Here's the code:
<?
$userIP = $REMOTE_ADDR;
$userURL = $HTTP_REFERER;
$userURLj = $_GET['page'];
$userAgent = $HTTP_USER_AGENT;
$userRef = $_GET['ref'];
$dateTime = date('Y/m/d g:i:s A');
$fileName = "visitorData.txt";
$fileHandle = fopen($fileName, 'a') or die("can't open file");
fwrite($fileHandle, $dateTime);
fwrite($fileHandle, " - ### - ");
fwrite($fileHandle, $userIP);
fwrite($fileHandle, " - ### - ");
fwrite($fileHandle, $userURL);
fwrite($fileHandle, " - ### - ");
fwrite($fileHandle, $userRef);
fwrite($fileHandle, " - ### - ");
fwrite($fileHandle, $userURLj);
fwrite($fileHandle, "\n");
fclose($fileHandle);
$lines = file('visitorData.txt');
?>
Any ideas on why this would happen?
I'm thinking that the script is somehow called twice simultaneously, so that two instances of the same script are running at the same time. If this is the case, any tips on how to simulate a lock so that two different instances of the script can't write to the file at the same time?
Thanks!