Click here to Skip to main content
11,502,812 members (53,062 online)
Click here to Skip to main content

Fast ASP.NET Hit Counter with Full Digit Graphic File Support

, 4 Oct 2002 262.6K 5.2K 97
Rate this:
Please Sign up or sign in to vote.
Fully customizable and fast site hit counter with multiple counters supported and done without using cookies or any extra files or settings!
<!-- Download Links --> <!-- Article image -->

Sample Image - FastCustomHitCounter.jpg

<!-- Add the rest of your HTML here -->

My Task

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! Smile | :)

  1. I needed it to be capable of using any standard digits graphic that I wanted.
  2. It had to be able to count differently on multiple pages.
  3. It had to be FAST! No excess overhead by database calls or complex structures.
  4. 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
  5. 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. Smile | :)

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! Smile | :)


How to Use It

The source code works on the principle of adding code like this to your page:

&ltimg src="counter/counter.aspx?src=digits.gif&digits=5&id=countername"&gt

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 Smile | :) )

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! Smile | :)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

JediBaron
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
Questionhit counter Pin
Member 993267421-Mar-13 22:57
memberMember 993267421-Mar-13 22:57 
GeneralMy vote of 5 Pin
karensuhm15-Oct-11 16:01
memberkarensuhm15-Oct-11 16:01 
GeneralMy vote of 5 Pin
brother.gabriel30-Dec-10 8:38
memberbrother.gabriel30-Dec-10 8:38 
GeneralNot working in master page Pin
NoBullMan15-Apr-10 6:15
memberNoBullMan15-Apr-10 6:15 
GeneralWorking but have one issue Pin
dfrimmel24-Feb-10 23:13
memberdfrimmel24-Feb-10 23:13 
QuestionWorking - but not working Pin
xiecsuk11-Apr-09 8:00
memberxiecsuk11-Apr-09 8:00 
GeneralRegarding the non working hit counter Pin
kinacav1-Oct-08 3:04
memberkinacav1-Oct-08 3:04 
GeneralImage Not Showing outside of running in the IDE Pin
cwodrsret11-Apr-07 2:36
membercwodrsret11-Apr-07 2:36 
GeneralRe: Image Not Showing outside of running in the IDE Pin
tolsen6421-Jun-08 11:22
membertolsen6421-Jun-08 11:22 
GeneralRe: Image Not Showing outside of running in the IDE Pin
Gamil11-May-09 23:57
memberGamil11-May-09 23:57 
GeneralCan't show the image to the remote server.... Pin
tsimikkonen31-Mar-07 11:05
membertsimikkonen31-Mar-07 11:05 
Generali'cant display image Pin
winazerty9-Mar-07 22:09
memberwinazerty9-Mar-07 22:09 
QuestionHELP! Can't get it to work in Dreamweaver MX Pin
Leyse21-Sep-06 8:40
memberLeyse21-Sep-06 8:40 
GeneralShame it is ASP.NET Pin
acidedge29-May-06 9:09
memberacidedge29-May-06 9:09 
GeneralPLEASE HELP - FILE LOCATION ERROR !!!! Pin
towelhead12-Jan-06 20:40
membertowelhead12-Jan-06 20:40 
Generalintermittant success Pin
larrys32552-Sep-04 10:51
memberlarrys32552-Sep-04 10:51 
GeneralFile corruption Pin
drober5-Aug-04 7:43
sussdrober5-Aug-04 7:43 
QuestionTrying to implement on external server? Pin
Linq28-Jul-04 10:30
memberLinq28-Jul-04 10:30 
AnswerRe: Trying to implement on external server? Pin
Linq29-Jul-04 3:16
memberLinq29-Jul-04 3:16 
GeneralRe: Trying to implement on external server? Pin
Linq29-Jul-04 3:32
memberLinq29-Jul-04 3:32 
GeneralHaving Problem Not working for me Pin
ASPCAT3-Jul-04 7:59
memberASPCAT3-Jul-04 7:59 
GeneralDoes Apache support ASPX Pin
BDavis09625-May-04 5:31
memberBDavis09625-May-04 5:31 
GeneralRe: Does Apache support ASPX Pin
Jason33311-Oct-04 8:48
memberJason33311-Oct-04 8:48 
GeneralRe: Does Apache support ASPX Pin
mondog2k527-Dec-05 6:09
membermondog2k527-Dec-05 6:09 
GeneralIt is working 100% Pin
fweinrebe20-May-04 22:47
memberfweinrebe20-May-04 22:47 
GeneralNot Working for me Pin
beland667-May-04 11:13
memberbeland667-May-04 11:13 
QuestionWas this procedure for applying this code ever perfected? Pin
appdar13-Jul-07 5:43
memberappdar13-Jul-07 5:43 
GeneralHit Counter ASP.net. I can't make it work. Please Help. Thank You. Pin
27lamps28-Jan-04 1:53
member27lamps28-Jan-04 1:53 
Generalstealinurforms Pin
Steve ParmIey23-Dec-03 6:02
memberSteve ParmIey23-Dec-03 6:02 
GeneralMore counter pix Pin
Steve ParmIey16-Dec-03 2:42
memberSteve ParmIey16-Dec-03 2:42 
GeneralRe: More counter pix Pin
Steve ParmIey17-Dec-03 17:22
memberSteve ParmIey17-Dec-03 17:22 
GeneralApplication locking and optimization ... Pin
Matt Casto24-Sep-03 9:43
memberMatt Casto24-Sep-03 9:43 
QuestionRe: Application locking and optimization ... Pin
pshanks2-Jun-06 6:59
memberpshanks2-Jun-06 6:59 
AnswerRe: Application locking and optimization ... Pin
Matt Casto2-Jun-06 9:16
memberMatt Casto2-Jun-06 9:16 
QuestionWhat about Load-Balanced sites Pin
Soliant11-Feb-03 17:41
memberSoliant11-Feb-03 17:41 
AnswerRe: What about Load-Balanced sites Pin
Heath Stewart30-Apr-03 17:55
editorHeath Stewart30-Apr-03 17:55 
GeneralInteresting Pin
mikestu4-Feb-03 13:59
membermikestu4-Feb-03 13:59 
GeneralRe: Interesting Pin
endforward9-Feb-03 8:17
memberendforward9-Feb-03 8:17 
GeneralRe: Interesting Pin
blong6-Mar-03 14:25
memberblong6-Mar-03 14:25 
GeneralOnly part I don't like about this Pin
Tony Truong3-Feb-03 0:29
memberTony Truong3-Feb-03 0:29 
GeneralRe: Only part I don't like about this Pin
TheSnarf23-Dec-04 3:44
memberTheSnarf23-Dec-04 3:44 
GeneralNot working for me. Pin
hkpillai5-Jan-03 21:17
memberhkpillai5-Jan-03 21:17 
GeneralRe: Not working for me. Pin
JediBaron8-Jan-03 7:32
memberJediBaron8-Jan-03 7:32 
GeneralRe: Not working for me. Pin
Shawn Weiler28-Jan-03 15:42
memberShawn Weiler28-Jan-03 15:42 
GeneralRe: Not working for me. Pin
JediBaron28-Jan-03 18:41
memberJediBaron28-Jan-03 18:41 
GeneralRe: Not working for me. Pin
Shawn Weiler29-Jan-03 13:06
memberShawn Weiler29-Jan-03 13:06 
GeneralRe: Not working for me. Pin
TracyE11-Dec-03 8:16
memberTracyE11-Dec-03 8:16 
GeneralRe: Not working for me. Pin
Member 381277510-Jan-08 8:47
memberMember 381277510-Jan-08 8:47 
GeneralCreating digit sets... Pin
bryonb23-Dec-02 9:09
memberbryonb23-Dec-02 9:09 
GeneralRe: Creating digit sets... Pin
JediBaron31-Dec-02 8:29
memberJediBaron31-Dec-02 8:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 5 Oct 2002
Article Copyright 2002 by JediBaron
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid