Creating a Hit Counter [PHP] - Version 1.0 by: GamingW[PHP][Coding]

Started by: GamingWarrior | Replies: 2 | Views: 1,370

GamingWarrior
Banned

Posts: 70
Joined: Oct 2007
Rep: 10

View Profile
Dec 29, 2007 1:54 AM #74627
HitCounter - Flat File - DOWNLOAD TUTORIAL

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.


######################################################
#// 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




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.

You can download this tutorial on top of the page.


Überschall
2

Posts: 3,607
Joined: Sep 2006
Rep: 10

View Profile
Dec 29, 2007 1:57 AM #74628
Good tutorial. I don't need it, but it's good.
GamingWarrior
Banned

Posts: 70
Joined: Oct 2007
Rep: 10

View Profile
Dec 29, 2007 2:05 AM #74629
Quote from Überschall
Good tutorial. I don't need it, but it's good.


Thankyou, I usually post em on my site for others, but I would like to share it with everyone over here. Just in case you need one...

Cheers, and Enjoy :)