PHP : Simple counter #2 with hit saving to .dat file

This is a discussion on PHP : Simple counter #2 with hit saving to .dat file within the Tutorials & Stuff! forums, part of the KP's Network Forum category; A counter is an essential part of any website. In this tutorial we will use PHP to create our own ...

Try Our customized MMoRPG experience, using World of Warcraft Client to connect
  

Reply

 

LinkBack Thread Tools
Old October 11th, 2004   #1 (permalink)
KingPin
Da Boss!
Starship Legend Champion
 
KingPin's Avatar
 
Join Date: Apr 2004
Location: Brooklyn, NY
Posts: 7,540
Thanks: 53
Thanked 72 Times in 50 Posts
Blog Entries: 24
My Mood: Lurking
KingPin KingPin o.OKingPin KingPin o.OKingPin KingPin o.O
KingPin KingPin o.OKingPin KingPin o.OKingPin KingPin o.OKingPin KingPin o.OKingPin KingPin o.OKingPin KingPin o.OKingPin KingPin o.OKingPin KingPin o.OKingPin KingPin o.OKingPin KingPin o.OKingPin KingPin o.O
PHP : Simple counter #2 with hit saving to .dat file

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.
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.
$new_count = fgets($exist_file, 255);

Now we want to add 1 to the variable and close the file.
$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.
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:
$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:
}
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:
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);
}
?>

Heaven doesn't want me, and hell is afraid I'll take over.NO SUPPORT VIA PM.

KingPin is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old May 25th, 2005   #2 (permalink)
.:XFX:. Member
 
blackops's Avatar
 
Join Date: Jan 2005
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
blackops is an unknown quantity at this point
PHP : Simple counter #2 with hit saving to .dat file

DONE
blackops is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks

Tags
php, simple, counter, hit, saving, dat, file


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 22:22.

no new posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89