Create a very simple hit page view counter without any database! Use flat file scripting to make an easy counter!
For this tutorial, I assume you have basic knowledge of PHP (if, echo, die, variables, etc).
Here is our code, I'll explain this piece by piece:
| <?php $file = 'hits.txt'; if (!is_writable($file)) die('not writable'); $current = trim(file_get_contents($file)) + 1; fwrite(fopen($file, 'w'), $current); echo $current; ?> |
This is our hit file variable, make sure this file exists, and make it contain "0".
| $file = 'hits.txt'; |
This checks to see if we can write to $file, which is "hits.txt".
| if (!is_writable($file)) die('not writable'); |
This we use two functions, trim and file_get_contents.
| $current = trim(file_get_contents($file)) + 1; |
trim will remove all whitespace in a string, and our string will be determined by file_get_contents.
file_get_contents will return the contents of a file into a string.
Then we use + 1 at the end, which will add 1 to the string, which would be the number inside of $file.
Now we're going to use fwrite and fopen to write the new hit count to $file.
fwrite will write a string into a file, controlled by a file handler (fopen).
fopen will create a file handler, pointed at a specified file and mode, we'll use "w" as the mode (write).
Then we just use echo $current to display our total hits.
That's all there is for this tutorial, enjoy it everyone!
Related Articles
- Hit CounterHit counters are used to track the amount of visitors there have been to a cetain page. I have looked through the internet and I have come up with 2 different ways to produce a hit counter.
- Simple Counter & Redirecting & Browser TypeThis is a really 2 file way of making a unique hit counter.
If you make a new file called counter.txt and just type the number 1 on the first line. - Counter in ASPA hit counter is an essential part of a site to know how many people are coming to site. Here is an easy way to make a counter. All you need is access to ASP and be able to give a write access to a directory...
- Simple Hit CounterUse this simple script for a basic click counter to display the amount of hits to your site.
- PHP CounterA counter is a simple yet essential script when developing web pages.
First of all, we need a file where to store the data ( a simple text file). This file must be CHMODed to 777 (under Linux)
Lets name this file data.txt. Do not forget to include it into the same directory as the PH... - A Nice Hit CounterI shows
Total Hits Unique Hits Todays Hits Todays Unique Hits - AIM Profile Screenname Viewer as a component of AIM Profile DesignsAim Profile Designs constantly develop. Read ASP Tutorial: AIM Profile Screenname Viewer. This tutorial will show you how to make a script to see who has seen your AOL Instant Messenger profile.
- Download Counter with Apache and PHPInfo: Collect statistics on the popularity of your downloads with Apaches mod_rewrite and PHP. Some sites present you an URI like Example.com, which is less than perfect. Being a Windows user (well sometimes) I expect that when I add a file to my download manager Ill see the filename in the list.....
- Common Elements for Website Pages (Exclusive Article)Among the common elements for all website pages, I have in mind those elements which are on all pages of the site, elements such as the menu, a tablet with counters, ?the hat?. Often with such elements there can be BIG problems. For example, youve decided to place a new counter, youve registered.....
- Conclusion: Stocking your ToolboxAs any good technician knows, there is no such thing as a best tool. The best tool is dependent on a whole host of factors from the type of task at hand to the personality of the marketing director. The best tool is a fantasy. Instead, every web developer should have at her disposal a wide a.....
