hitCount() - Count hits to a text file.

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

Code: 

<?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;           
        }
    }
 
?>

jrchew's picture

If you're getting permission

If you're getting permission errors don't forget to chmod 777 the path to the directory to the script. Also don't forget to create the count.txt file and chmod 777 it as well.

JR Chew
Founder & CEO
Creative (ctrl)

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
Are you human?
Image CAPTCHA
Enter the characters shown in the image.
X
Loading