Creative (ctrl) Code Refactor

jrchew's picture
jrchew

This is a simple hit counting function that:

1.) Looks for a file named count.txt
2.) Upon finding the file this function either adds the numeric value of 1 to the beginning of the file or adds 1 to the current value within the file.

Enjoy - JR

<?php
 
function hitCount()
    {
        if(
file_exists('count.txt'))
        {
           
$file = fopen('count.txt', 'r');
           
$data = fread($file, filesize('count.txt'));
           
fclose($file);
           
$file = fopen('count.txt', 'w');
           
fwrite($file, $data+1);
            return
$data+1;
        }
        else
        {
           
$file = fopen('count.txt', w);
           
fwrite($file,1);
           
fclose($file);
            return
1;           
        }
    }
 
?>

X
Loading