A simple hitcounter

Hi there!
Today I'm going to show you how to make a simple hitcounter to keep track of how many hits your website gets.
It wont count unique hits, -meaning that it will add 1 hit everytime the page loads.

We will be storing the hits inside a simple .txt file so we wont be needing much code.
First of all, we ought to set up the files we need.
So create 1 .php file and 1 .txt file called "hits.txt".
Leave the php file blank but put "0" inside the .txt file and save it.

I might as well show you the whole script now, and comment it inside the code.

<?php $filename = "hits.txt"; //The name of the file you are storing the hits in. $handle = fopen($filename, "r"); //Opens the file for reading. $views = fread($handle, filesize($filename)); //Gets the content fclose($handle); //Closes the file. $handle = fopen($filename, "w"); //Opens the file again, but for writing. $views++; //Adds 1 hit fwrite($handle, $views); //Writes the new amount of hits to the file. fclose($handle); //Closes the file. echo $views; //Outputs the number of hits. ?>

- This is of course the code you have to put inside the .php file.

Test the script and if it don't work, you might have to chmod it to 777.

If you want to learn more about reading and writing to files I strongly suggest that you visit www.php.net/fopen
and read from there.

Want to get in touch? Got some feedback for me? or a couple of requests? Follow me at twitter:

Need a reliable webhost? Take my word for it and go with Hostgator Green Web Hosting by HostGator

Warning: fread(): Length parameter must be greater than 0 in /customers/f/2/7/n1studios.net/httpd.www/minicounter.php on line 17