The website needs to display the number of online visitors

Source: Internet
Author: User

Customers have never had this requirement for Insus. NET to develop such websites. However, some customers have such requirements.

Number of online user visits. That is to say, to write a counter for the website, the initial value of the counter is 0. When the website starts running (Application_Start), statistics are started, when a user accesses the server (Session_Start), the counter is incremented by 1. When the user leaves the server (Session_End), the counter is reduced by 1.

On the website, there is a file named Global. asax:



Define a counter at the beginning of the program. The initial value is 0.

Application_Start (sender, e) = View Code


When a user accesses a website:

Session_Start (sender, e) = (Application (),) + View Code


When a user leaves the website:

Session_End (sender, e) = (Application (),)-View Code


In the above two Session_Start and Session_End methods, Insus. NET has Application. lock and Application. the Unlock method is used to prevent multiple threads from changing this variable at the same time. When changing the counter, Lock the variable first, and then Unlock the variable.


Save the Global. asax file and display the number of online visitors on the webpage:

 

 
During the test, Insus. NET uses two browsers to allow websites to access different process visitors. Each browser opens a different window and obtains the data variables.

 

Postscript:
This method is just a superficial effort, and cannot count the real number of online visitors. To achieve online statistics, you must obtain other parameters of the visitor.

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.