Important
It is worth noting that this tutorial is paginated, and that the entire tutorial contains 4 pages. Just remember that the tutorial is not finished just after the first page!
PHP 4+ is required for this tutorial. PHP 5 is optional
This tutorial is for beginners with very limited knowledge of PHP. You will learn to make a very simple hit counter. The counter isn’t important — it’s the techniques used that you should try to remember. Something similar will pop up later on, so try to remember what was said in this tutorial.
If you are rather experienced with PHP, you still can follow along. It never hurts to brush up on some old easy stuff. I will show you how to create a hit counter (not uniques, just simple hits) using a flatfile. Alright, let’s get started.
Step 1 A
Open your text editor and create a new document. I will use NotePad as an example here. Before you even start typing a single thing, press File -> Save As… and save your document first. We want to create all our files first before starting the code.
I saved my file as “counter.php“. The name doesn’t matter, but make sure you don’t forget the .php extension.

Step 1 B
Now, we need to create a new file to store the number of hits. This is called a flat file. A flat file is similar to a database, but it is a file instead. Press File -> New. This new file will consist of nothing but a number. This number will be the hit count. Because we’ve just started, type “0” in the document and press File->Save As…. Save this file as a TXT document. I named mine “hits.txt”, but you can name it differently.

Step 2
Alright, now that we’ve got our files set up, lets start codin’. Open up counter.php (or whatever you named it) and type the PHP tags.
<?php
?>
Step 3
Now, we open up the flat file (hits.txt) and tell PHP to read the number inside and set it as a variable (we’ve already put “0″ into hits.txt)
<?php
$hits = file_get_contents("hits.txt");
?> The function file_get_contents reads all the text in the file that is quoted in the first argument (the first argument being hits.txt in this case) and puts it into a string. A string is simply data. It could be text, numbers, anything. In this case, it is only one thing — a number.
When we put “$hits =”, this means we are defining a variable. In this case, we are saying that the variable $hits (all variables must have a dollar sign before it) is equal to everything inside hits.txt (which in this case, is only “0″).
Step 4
Because we are viewing the file, we have to add a new hit to the counter. Take a look at the following code and try to figure out yourself what it means:
<?php
$hits = file_get_contents("hits.txt");
$hits = $hits + 1;
?> The new line that I’ve added — “$hits = $hits + 1;” should be easy to decipher. I am redefining the variable $hits. This time, $hits is “$hits + 1″. The line basically says in plain english — $hits should now be whatever’s inside the file “hits.txt” plus one. What’s inside the file? Well, it was only 0… which means $hits should equal 0 + 1.. = 1.
Step 5
Now that we’ve updated the hits count to add one, lets update hits.txt to the NEW hits count.
Take a look at this code:
<?php
$hits = file_get_contents("hits.txt");
$hits = $hits + 1;
$handle = fopen("hits.txt", "w");
fwrite($handle, $hits);
fclose($handle);
?> The function fopen opens the file “hits.txt” in the first argument. The “w” tells PHP that we are only opening the file to write, and nothing else. It also tells PHP to erase everything in the file and set the pointer (something like little flashing black line you see when you type in a text box) to the beginning.
Why do we need a variable? Because we need a handle from fopen to execute other commands like — in this case — fwrite(). To write, you have to tell PHP that you’ve previously opened the file — and give PHP the handle. The line “fwrite($handle, $hits);” tells PHP to write to the file specified in the $handle (”hits.txt”) and it also tells PHP to write $hits into the file. $hits — as we’ve already discovered — is the old hit number plus one. Now you can see somewhat of a hit counter resemblance. Adding one hit to the old number.
fclose() tells PHP that we’re done with the file, so close it.
Side Note — in PHP 5, it is possible to fopen, fwrite and fclose all at once with the function file_put_contents
Step 6
This part’s the easiest — output the number.
<?php
$hits = file_get_contents("hits.txt");
$hits = $hits + 1;
$handle = fopen("hits.txt", "w");
fwrite($handle, $hits);
fclose($handle);
print
$hits;
?> It’s a no-brainer: “print $hits”. It prints out whatever’s in the variable $hits — in this case, it’s “1″. But as time goes on, when we execute this script over and over again — it will always add 1 and so — this is a hit counter.
Completed
Now, assuming you saved this file as counter.php, in any other file you want to count hits — put
<?php include "counter.php"; ?> This will include the counter file — which will only output a number, and nothing else. You can put something like
There are <?php include “counter.php”; ?> hits.
or something like that.
