Heres a commented version of a 'hit logger'
code:
<?php
// Define the text file to store data
$HitFile = "logger.txt";
// Open the file for reading and writing
$handle = fopen($HitFile, "r+");
// Lock the file for exclusive access (protects from data coruption)
flock($handle, LOCK_EX);
// Get the contents of the file (the old hit count)
$oldcount = fgets($handle);
// Increment 1 to the old count
$newcount = $oldcount + 1;
// Set the file head to the begining (destroys the old data)
rewind($handle);
// Write the new count to the file
fwrite($handle, $newcount);
// Unlock the file
flock($handle, LOCK_UN);
// Close the file
fclose($handle);
// Display the new hit count
echo $newcount;
?>
logger.txt should have 0 to begin with