<!-- Download Links -->
<!-- Article image -->
<!-- Add the rest of your HTML here -->
Basically I set out to create a nice graphically customizable counter using VB.NET and ASP.NET. After searching the web for a bit I realized that there wasn't really any good ASP.NET hit counter programs out there so I just had to make my own! :)
- I needed it to be capable of using any standard digits graphic that I wanted.
- It had to be able to count differently on multiple pages.
- It had to be FAST! No excess overhead by database calls or complex structures.
- It had to use session and application variables to keep track of hits - no cookies. I Needed an app that would only count each unique session and not every page refresh or not work on machines without cookies
- Most importantly, it had to be completely self encapsulated without me having to make any other supporting code from any part of the rest of my web site.
How It Works
You will find that the source is very well documented so there is really no need to go into it here. Specially for those who just wish to use the code. :)
Here is a generic flow of how it works in principal:
- Get the current counter value from an application variable named, in part,from the ID. Since this is in an application variable, if someone else came to your site after say you did it would count just perfectly. ie - User 1 starts a new session at the site - counter goes fom 100 to 101, then User 2 starts a session with the site and counter goes to 102, then the first user refreshes his page and his counter changes to 102 also. Very cool.
- Check if a temp session variable exists that is also based on the ID. If it doesn't then get the current count from text file (in case someone has changed it manually). Then increment the counter and save it to the application variable and the text file. Lastly setting the temp session variable so that the counter will not have to access the text file or increment the counter for the rest of the current user's session (most servers set the session timeout to 15 mins so the user will either have to close out his browsers and make a new session or not do anything on the site for 15 minutes before the system will count him again). This check here also only reads or writes to the text file when the counter is incrementing! Therefor any kind of delays in generating the counter due to only one person being able to write to the text file at a time are GREATLY limited - making the counter ultra fast even if you have 1000's of viewers in your site at the same time
- Using the awesome graphic capabilities of .NET, the program then loads up the digit graphic (BTW - any graphic format that .NET natively supports will work -Gif,Jpg,etc.) and procedes to pull out the digits one at a time and build the new counter graphic. After it's built it simply returns the image to the caller.
Sort, simple, and extremely effective like all good program components should be! :)
How to Use It
The source code works on the principle of adding code like this to your page:
counter/counter.aspx is the location and name of the program. (I highly recommend putting the counter program in it's own directory in your site so that it keeps your site clean :) )
src is set to whatever image file contains your counter's digits. The image location is relative to the location of the counter.aspx so I recommend just puting all of your different digit graphics in the same directory as the counter. The image must be in the standard 10 digit format (0 through 9) and all the digits must be of the same width as the program automatically gets the width of the digits by dividing the image width by 10. And the height of the digits is set by the height of the image. This is standard by all normal downloadable digit pics. There are many sites that you can get free digit images from (ie Click here for a big sampling)
digits is just simply set to the number of digits you want your counter to show.
id is a unique id to identify your counter. You just set this to anything to name your counter although you will have keep it to normal text cause your counter's text file will be named after it (not to mention the internal session and application variables that it uses). This feature lets you have many different counters in your site cause you can just name them differently.
As you will see when you start using it - it simply keeps the current count backed up in a text file named after the counter name. This allows you to set an initial value or modify the value to whatever you want.
That's all there is to it - ENJOY ALL! :)