You could create a function that uses fwrite
to write your events to a log file.
eg:
function logEvent($message) {
if ($message != '') {
// Add a timestamp to the start of the $message
$message = date("Y/m/d H:i:s").': '.$message;
$fp = fopen('/path/to/log.txt', 'a');
fwrite($fp, $message."\n");
fclose($fp);
}
}
You should also add checks that the file is writeable, which you can find code for on the fwrite
page.
For each of the events that you mention, you can then do:
// Log that user has started playing
logEvent('User '.$user.' has started playing Battleships.');
Expand it to include/do whatever you want.
Edit: You've edited your question, but I'll leave my answer here because you may find it useful. As others have mentioned though, you really can't reliably tell when someone has closed the browser window. Also changed to append rather than override.