. NET-based website access statistics counters

Source: Internet
Author: User

You can use Global. asax Global variables to avoid refreshing counters on the page.

Void Application_Start (object sender, EventArgs e)
{
// The code that runs when the application is started, and the counter is refreshed when IIS is restarted.
Application ["count"] = 0;
}

Void Session_Start (object sender, EventArgs e)
{
// The code that runs when the new session starts. The session is established and the counter is + 1 to prevent the error from being locked first and then unlocked. Refresh the page repeatedly without changing the counter. You can only create a new session.
Application. Lock ();
Application ["count"] = (int) Application ["count"] + 1;
Application. UnLock ();
}

Void Session_End (object sender, EventArgs e)
{
// The code that runs when the session ends. This is mainly used to count the number of online users. If you only need to count, you do not need the code here.
// Note: Only the sessionstate mode in the Web. config file is set
// The Session_End event is triggered only when InProc is used. If the session Mode
// If it is set to StateServer or SQLServer, this event is not triggered.
Application. Lock ();
Application ["count"] = (int) Application ["count"]-1;
Application. UnLock ();
}

Protected void Page_Load (object sender, EventArgs e)
{

// Directly use Application ["count"] on the page.
Response. Write ("you are the website" + Application ["count"] + "visitor ");
}

Note: The difference between Application and Session. The former is the application variable, and the latter is the session variable. The former remains for a long time and cannot be used in disorder.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.