Thursday, December 13, 2007

Sometimes Faster From Scratch: An ASP.NET Hit Counter

Some users of a SharePoint site I maintain wanted to add a hit counter to their statically created HTML newsletters. After a little looking, I decided it would probably be best to use the FrontPage hit counter that is included with IIS/FrontPage Server Extensions. It was already there, after all, and should be easy to implement. Well, it was easy to implement, except for one major snag: it wouldn't fire unless you were authenticated.


Try as I might, I wasn't able to figure out how to get it to work for anonymous users. Google didn't turn up much. I even posted on the Microsoft newsgroups, and got no response. I spent a day and a half searching, trying this and that, and was no further along than when I had started. No, I take that back. I was further behind than when I had started, because I had exhausted most of the possibilities I had started out with.

Because I was sort of stuck, I did a cost-benefit analysis (which makes it sound much more scientific and rational than it actually was) of continuing to troubleshoot the problem. I came to the conclusion that I had already spent more time troubleshooting than it would have taken for me to develop a custom ASP.NET solution from scratch.

So I switched gears, and began searching for code samples on the web. Within minutes, I found one in Visual Basic that did most of what I wanted. Of course, I program in C#, so I merely used it as a reference. I improved it a little, too.

(You can download the VS2005 project here)

Here's how it works:

The project consists of a single Default.aspx page, and an Access database, hitcount.mdb, for tracking URLs and hits.

The Default.aspx page is passed an address, in the form

Default.aspx?url=some/path/on/a/server.htm

or even

Default.aspx?url=http://some/path/on/a/server.htm

In truth, you could just pass in some arbitrary text and it would use that as an identifier. Using a full URL, however, ensures that you don't get duplicates.

If the hit counter finds the URL in the database, it gets the count, increments it, converts it to a font (Arial), converts the font to an image, and returns as Content-Type "Image/Gif".

If it doesn't find the URL, it inserts it, and returns a count of 1. It's that simple.

The only thing you may need to change for it to work on your server is line 50:

string datapath = Server.MapPath("\\hitcount\\hitcount.mdb");

Set this to the directory on your web server where the database will live.

To use this in your static HTML pages, simply add a line like the following:

<img src="Default.aspx?url=some/path/on/a/server.htm" />


A note on security: there is no check in the code to prevent someone from "borrowing" the hitcounter and using it to store hits for their own pages. This might be a negligible concern, but you could add some code to check the IP address where the request is coming from if you are worried by this.

No comments:

Post a Comment