Tracking Unique Hits


Chapter 6 described how to use cookies to store users preferences. Cookies are handy because they are user specific (stored on the user s computer, not on the Web server) and because we can control how long they hang around on users computers.

Because cookies work so well for storing user information, we ll use them again with our hit counter. When users first run the page, we ll count them for our counter and we ll also write a cookie out to their computer. If the same users come back to the page, we ll see that they have a cookie already, and we ll know that we can ignore them (for purposes of counting only, of course).

You can use a cookie for both of the hit-counter scenarios, session- based and user-based. The only difference will be whether you specify an expiration date for the cookie. Remember that when you create a cookie, you usually set an expiration date. A cookie with an expiration date is written to the user s computer and lives there for the specified lifetime.

If you don t specify a cookie expiration date, the cookie becomes a session cookie. Its lifetime is limited to that of the user s session. When the user closes the browser or when the session times out, the cookie is discarded. Session cookies aren t even written to the user s computer; they don t need to be because they re part of the session that the Web server itself is keeping track of. Session cookies prove to be perfect for our session-based counter.

Note 

Remember that cookies don t work in every case. For one thing, users can refuse cookies. Users might also periodically delete all the cookies on their computer as part of general housecleaning. For the casual nature of what we re doing, these issues don t matter. A more accurate way to track users would be in a database, as noted earlier, in the section Counting Visitors to Your Pages.




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

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