C # Statistics website total number of visitors and current online application

Source: Internet
Author: User

First, open vitualstudio2010, create a new Web site, then add new items, create a new login.aspx and Index.aspx page. Add a new item, select the Global application class, and the page is global.asax.

First step: Open the Global.asax page and write the following code under the Application_Start () method:

Application.Lock ();

application["Count"]=0; Application.set ("Count", 0)/application.add ("Count", 0) initializes the variable, which is equivalent to setting count to 0.

application["Online"]=0;

Application.UnLock ();

Step Two: Write the following code in the Session_Start () method:

Application.Lock ();

Session.Timeout (1); Setting a session to a time of one minute, or no action within a minute, will invalidate the session.

application["Count"]= (int) application["Count"]+1;

application["Online"]= (int) application["online"]+1;

Application.UnLock ();

Step three: Write the following code in the Session_End () method:

Application.Lock ();

Session.Abandon (); When you end a session, log off the session

application["Online"]= (int) application["online"]-1;

Application.UnLock ();

Fourth step: Write the following code in Index.aspx.cs's Page_Load () method:

Response.Write ("<br/> Total number of visitors:" +application["Count"]. ToString ());

Response.wrirte ("<br/> is currently online": "+application["). ToString ());

The total number of visitors to the output and the number of people currently online can also be output through a Label control or other control, such as:

label1.text=application["Count"]. ToString () + "person"

label2.text=application["Online"]. ToString () + "person"

C # Statistics website total number of visitors and current online application

Related Article

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.