What is a Hit Counter FlatFile?
In this tutorial, I will step by step show you how to create a hit counter, to keep track of the traffic level on your site. But what is flat file? Flat file means that we are simply going to create a ".txt" file, and add the number "0" in it. Now everytime someone visits or reloads your page, our hit counter will open our ".txt", and add +1 each time. Once we finish that part, I will show you how to display the data from the ".txt" file.
What we will need?- You need the "counter.txt" file, with nothing but a "0" in it.
- A new page called show_counter.php
- And a server obviously.
STEP 1 - The code
This is the full preview of the code, I will explain each line in the tutorial.
Step 2 - Explaining the code
Alright, in the first line of the code you see a new variable we call "$file". Basically, all we are doing here is opening our file called "counter.txt", the "R+" allows us to both read and write into the file at the same time.
The next couple lines you will see the fread function, which basically reads the data from our txt file, with line int lengt of 512 lines. You can edit the line to 1 and it wont make a difference, unless you have a long complicated txt file.
As we echo the $get_data, you can see the $new_data. It adds a plus one to the old number, a simple equation, that I am positive you will understand.
Now that we can add data, and read the file, we want to update the txt file. We use the FSEEK function to go back to the FIRST line of the txt. With the fwrite function, we can update that data, and finally close the file.
NOTE: This tutorial is a simple copy paste job, nothing too hard, and please do not edit any information, and leave the credit there. Thankyou.- A new page called show_counter.php
- And a server obviously.
STEP 1 - The code
This is the full preview of the code, I will explain each line in the tutorial.
######################################################
#// Author: Antonio Dowlatkhahi
#// AllRights Reserved
######################################################
// This script will add a counter on your website
// First thing is first, we need to define where the
// data is
$file = fopen("counter.txt","r+");
// Open the file R+ (read and write)
$get_data = fread($file,512);
// Read the counter.txt
$new_data = $get_data + 1; // Adds 1 to the counter
echo $new_data; // Echo the Counter
fseek($file, 0) ;
//Go back to line 1 in our counter file
fwrite($file, $new_data) ;
// Save the new data
fclose($file) ;
//closes the file
?>
Step 2 - Explaining the code
Alright, in the first line of the code you see a new variable we call "$file". Basically, all we are doing here is opening our file called "counter.txt", the "R+" allows us to both read and write into the file at the same time.
$file = fopen("counter.txt","r+");
// Open the file R+ (read and write)
The next couple lines you will see the fread function, which basically reads the data from our txt file, with line int lengt of 512 lines. You can edit the line to 1 and it wont make a difference, unless you have a long complicated txt file.
As we echo the $get_data, you can see the $new_data. It adds a plus one to the old number, a simple equation, that I am positive you will understand.
Now that we can add data, and read the file, we want to update the txt file. We use the FSEEK function to go back to the FIRST line of the txt. With the fwrite function, we can update that data, and finally close the file.
$get_data = fread($file,512);
// Read the counter.txt
$new_data = $get_data + 1; // Adds 1 to the counter
echo $new_data; // Echo the Counter
fseek($file, 0) ;
//Go back to line 1 in our counter file
fwrite($file, $new_data) ;
// Save the new data
fclose($file) ;
//closes the file
You can download this tutorial on top of the page.