A Simple Text-File Hit Counter : fputs « File Directory « PHP






A Simple Text-File Hit Counter

 
<?php
    function retrieveCount($hitfile) {
        $fr = @fopen($hitfile, 'r');
        if(!$fr) {
            $count = 0;
        } else {
            $count = fgets($fr, 4096);
            fclose($fr);
        }
        $fr = @fopen($hitfile, 'w');
        if(!$fr) return false;

        $count++;
        if(@fputs($fr, $count) == -1) return false;
        fclose($fr);

        return $count;
    }

    $count = retrieveCount('hitcount.dat');
    if($count !== false) {
        echo "This page has been visited $count times<BR>";
    } else {
        echo "An Error has occurred.<BR>";
    }
?>
  
  








Related examples in the same category

1.Using the fputs() Function
2.Writing and Appending to a File
3.Storing data on a remote server