p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Classic ASP Basics (http://p2p.wrox.com/forumdisplay.php?f=61)
-   -   Page Hit Counter (http://p2p.wrox.com/showthread.php?t=5660)

shmacgregor November 2nd, 2003 12:53 AM

Page Hit Counter

How do I correctly code a Site Hit Counter for the number of times (sessions) a page has been visited.
Reference is Professional ASP.NET 1.0, pages 610 and 611.
In Global.aspx.cs I've inserted the second line with intHitCount
    public class Global : System.Web.HttpApplication {
        protected int intHitCount = 0;

    protected void Session_Start(Object sender, EventArgs e) {
        intHitCount += 1;
        Session["SessionHitCounter"] = intHitCount.ToString();

Then in the Welcome.aspx page file I have:
<form id="Form1" method="post" runat="server">
<h3 class="title"><i>Welcome! You are visitor number</i><br>
<asp:label id="lblHitNo" runat="server" Width="129px" BackColor="#dbdbdd" ForeColor="#ef0063"></asp:label><br>
<i>since November 2003</i></h3>

and in the Welcome.aspx.cs file I have:
public class WelcomeForm : System.Web.UI.Page {
    protected System.Web.UI.WebControls.Label lblHitNo;
    protected System.Web.UI.HtmlControls.HtmlForm Form1;

    private void Page_Load(object sender, System.EventArgs e) {
        lblHitNo.Text = (string)Session["SessionHitCounter"];

and this does NOT work. Strangely, it counts up to about 85 and then starts over.



Imar November 2nd, 2003 10:45 AM

Hi Sandy,

Are you running Windows Server 2003? In II6 the option to recycle your Web application every X minutes / hours is on by default. This may cause the loss of information.

If that's the case, you may need to disable this option, or store the counters in a durable location, like a database or text-file.

Can you please post your posts in the relevant forum? This forum is for Classic ASP and not for .NET. This is also true for the cross posts you made in the HTML forum.



Imar Spaanjaars
Everyone is unique, except for me.

shmacgregor November 2nd, 2003 02:24 PM


Thank you so much for responding.
1. Which topic is the correct topic for ASP.NET 1.0?
2. My development platform is XP Professional, Version 2002, Service Pack 1, with IIS 5.1 and SQL Server 2000. I think the Web Server it's on is Windows Server 2000 - I'll check Monday morning and let you know in the correct topic.


Imar November 2nd, 2003 03:09 PM

It all depends on the topic you're posting, but ASPX Beginners or ASPX Professional are good candidates:


When you say it counts to 85, how do you get to that count? Do you continuously refresh the page, or do you wait a while. And if you open the page in a fresh browser instance, does it take on the old value, or does it start at 1?


Imar Spaanjaars
Everyone is unique, except for me.

All times are GMT -4. The time now is 04:55 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.