Counter-without-a-Database

Any problem with PHP can be disscused here
Post Reply
ledung
Posts: 4
Joined: Tue Feb 15, 2005 12:01 pm

Counter-without-a-Database

Post by ledung »

A counter is an essential part of any website. In this tutorial we will use PHP to create our own counter. Given that not everyone has access to a database, this tutorial will save the hits to a .dat file. If you would a more advanced counter for your site i recommend signing up for the ND Counters service.
Lets start by using the file_exists() function to check if the counter.dat file exists. If it does then we are going to open it using the fopen() function.

Code: Select all

if(file_exists("counter.dat"))
{
$exist_file = fopen("counter.dat", "r");
Notice the r that was added into the fopen() function. This r means that the file counter.dat will be opened for reading only. There are several other options that can be placed here, we will cover those at the end of the tutorial, however for our purposes we only need to open the file as read-only.

Now we are going to use the fgets() function in order to find out how many hits are in the counter.dat file. We are going to save the command we use to find the hits into a variable (we'll call it new_count), this way we can access it later on.

Code: Select all

$new_count = fgets($exist_file, 255); 
Now we want to add 1 to the variable and close the file.

Code: Select all

$new_count++;
fclose($exist_file); 
Now we want to display the number of hits to our users. To accomplish this we are going to use the print() function.

Code: Select all

print("$new_count people have visited this page"); 
Now that we have increased the hits by 1 we want to save this new number into the counter.dat file and then close it. Weare going to use the fputs() function to accomplish this:

Code: Select all

$exist_count = fopen("counter.dat", "w");
fputs($exist_count, $new_count);
fclose($exist_count); 
Above we saved the fopen() function to the $exist_count variable. The w inside the fopen() function menas that the file counter.dat will be opened for writing only.

Now we are going to close the if section and work on the else section. Incase the counter.dat file doesn't exist we are going to let PHP create it for us. To do this we use the following code:

Code: Select all

}
else
{
$new_file = fopen("counter.dat", "w");
fputs($new_file, "1");
print("1 person have visited this page");
fclose($new_file);
} 
Now lets put all of this together:

Code: Select all

<?
if(file_exists("counter.dat"))
{
$exist_file = fopen("counter.dat", "r");
$new_count = fgets($exist_file, 255);
$new_count++;
fclose($exist_file);
print("$new_count people have visited this page");
$exist_count = fopen("counter.dat", "w");
fputs($exist_count, $new_count);
fclose($exist_count);
}
else
{
$new_file = fopen("counter.dat", "w");
fputs($new_file, "1");
print("1 person have visited this page");
fclose($new_file);
}
?> 


--------------------------------------------------------------------------------

Now as promised lets quickly cover all of the options that you can place when using the fopen() function. Quoting from the PHP Manual:
'r' - Open for reading only; place the file pointer at the beginning of the file.
'r+' - Open for reading and writing; place the file pointer at the beginning of the file.
'w' - Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
'w+' - Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
'a' - Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
'a+' - Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.


Post Reply