Testing the Counter


Time to see the counter in action, finally. We ll run the page so you can see what the counter looks like and, importantly, how well our page works to update the counter. Perversely, before you even start testing, I m going to have you change your code, but for a good reason.

Test the hit counter as a session counter

  1. In the Page_Load handler, use an apostrophe to comment out the following lines so that they aren t executed:

     Response.Cookies("HitCounter_Cookies").Expire s =       Now.AddMonths(1)

    Commenting out these two lines turns your cookie into a session cookie temporarily. This will make it easier to test whether the counter is working.

  2. Press F5 to run the page.

    Assuming you get no errors, you ll see the page with the initial counter value.

  3. Close the browser, which will end your session.

  4. In Web Matrix, press F5 to run the page again.

    You ll have a new session this time, so you ll be counted again and the counter will be incremented.

  5. Close the browser.

Note 

If you ve been testing as you go along, you might already have a cookie, so the session test won t work. If so, you can delete cookies in Internet Explorer. From the Tools menu, choose Internet Options, and on the General tab, click Delete Cookies. If you don t want to delete all your cookies, you can delete just one. On the General tab in the Internet Options dialog box, click Settings under Temporary Internet Files. In the Settings dialog box, click View Files. The cookie you created with the example in this chapter will be one of the most recently updated files. The cookie will be in a file named Cookie:<yourname>@localhost. Delete that cookie and try the test again.

By reversing the simple change you just made, we can turn the counter back into a unique visitors counter.

Test the hit counter as a unique visitors counter

  1. In the Page_Load event handler, remove the apostrophe from in front of the following lines:

    Response.Cookies("HitCounter_Cookies").Expires  =      Now.AddMonths(1)
  2. As a temporary measure, change the expiration date of the cookie from one month to ten seconds. Assigning a short expiration date allows us to test the expiration of the cookie without waiting a month. Change the line to read as follows, with the changed part shown here in boldface:

    Response.Cookies("HitCounter_Cookies").Expires  =      Now.AddSeconds(10) 

    Obviously, since you re now setting an expiration date for the cookie, it will be stored on the user s computer.

  3. Press F5 to run the page. The counter will be incremented.

  4. Close the browser.

  5. In Web Matrix, press F5 to run the page again. If you ve done so within 10 seconds of running the page last time, the value of the counter will be the same.

  6. Close the browser again.

  7. Wait a minute or so and then run the page one more time. This time you should see the counter incremented, because the cookie has expired.

    You can leave the page with whichever type of counter you want to use, session or unique visitors. If you want to track unique visitors and not just sessions, change the cookie expiration date to a long time period such as a month.




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