Chapter 8: Creating a Page Hit Counter


In this chapter, I ll show you how to create a Web page with a hit counter that tracks how many times the page has been visited. Visitors will be able to see how popular your site is and will perhaps be tempted to visit your site again. You ll learn a variety of programming techniques, including how to write information to a text file. You ll also learn some techniques for working with strings (characters), such as how to display numbers as strings and how to get a portion of a string (a substring) out of a larger string. I ll explain how to create controls on the page programmatically, rather than dragging them from the Toolbox in Web Matrix. Finally, I ll illustrate a simple but effective way to create graphical numbers that is, numbers in which the individual digits are made up from graphics files.

Counting Visitors to Your Pages

Including a hit counter on a Web page that shows people how many times the page has been visited can be useful and fun. Figure 8-1 shows an example hit counter.

click to expand
Figure 8-1: A hit counter showing how many times a page has been visited.

If you develop a page that s popular, the hits can mount up very rapidly indeed. But even if your page gets only a few visitors, creating a hit counter can be an interesting exercise because creating a functional hit counter provides you with practical knowledge of how Web pages work.

You can add a hit counter to your page without actually developing the counter yourself. (See the following sidebar Commercial Hit Counters. ) By developing the hit counter yourself, however, you get to customize the counter s appearance as well as define what constitutes a hit. You also learn some useful Microsoft ASP.NET programming techniques that will doubtless come in handy for other purposes.

The hit counter we develop in this chapter will not provide any detailed statistics, such as when your page was visited or by how many different users. For that we would need to track information in a database, and we haven t gotten to databases yet. For now, you ll be literally counting hits, period.

start sidebar
Commercial Hit Counters

I should point out that you can add hit counters to your page easily by obtaining a hit-counter service. A quick search of the Web will turn up at least a dozen. You can usually add these services to any page; the page doesn t have to be an ASP.NET page, and it doesn t matter what Internet service provider (ISP) you re using.

The hit-counter services usually work this way: you use an <img> tag and point to the Web site of the hit-counter service for the image source (the src attribute). The following example points to the Microsoft bCentral site:

<img border="0" src     "http://fastcounter.bcentral.com/fastcounter?025+598">

The hit-counter service tracks the requests to its own Web site via the <img> tag on your page. The service then dynamically generates a hit- counter image, such as an odometer, and sends it to the <img> tag as the graphic to display. In this chapter, I ll show you how to create a hit counter that looks similar to a commercial one.

end sidebar



Microsoft ASP. NET Web Matrix Starter Kit
Microsoft ASP.NET Web Matrix Starter Kit (Bpg-Other)
ISBN: 0735618569
EAN: 2147483647
Year: 2003
Pages: 169
Authors: Mike Pope
BUY ON AMAZON

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net