The website needs to show the number of online visitors

Source: Internet
Author: User

Insus.net the development of such a multi-site, customers have not this requirement. However, now there are customers have such a request.

Online user access, that is, to write a counter for the site, the initial value of the counter is 0, when the site started running (Application_Start), the start of statistics, when there is a user access (Session_Start) counter plus 1, when the user access to leave ( Session_End) counter minus 1.

In the Web site, there is a file called Global.asax:



At the beginning of the program, define a counter with an initial value of 0

Sub Application_Start (ByValasObjectByVal as EventArgs)         ' Code that runs on application startup                 Application ("onlinevisitors"0    End Sub 
View Code


When a user visits a website:

SubSession_Start (ByValSender as Object,ByValE asEventArgs)'Code that runs when a new session is startedApplication.Lock () application ("onlinevisitors") =DirectCast(Application ("onlinevisitors"),Integer) +1Application.UnLock ()End Sub
View Code


When a user leaves the site:

SubSession_End (ByValSender as Object,ByValE asEventArgs)'Code that runs when a session ends.        'note:the Session_End event is raised only when the sessionstate mode        'is set to InProc in the Web. config file. If session mode is set to StateServer        'or SQL Server, the event is not raised.Application.Lock () application ("onlinevisitors") =DirectCast(Application ("onlinevisitors"),Integer) -1Application.UnLock ()End Sub
View Code


Of the above two Session_Start and Session_End methods, Insus.net has the use of Application.Lock and Application.UnLock method, is to prevent multiple threads at the same time change this variable, when changing the counter, first lock it up, notconsistent complete, and then unlock.


To save the Global.asax file, you need to show the location of the number of online visitors on the Web page:

<% = Application ("onlinevisitors"). ToString ()%>


During the test, the insus.net used two browsers to make the site get different process visitors. While each browser opens a different window, the data variable is obtained.

Postscript:
This method, only to do surface Kung Fu, statistics can not really online visitors. If you need to really achieve online demographics, you need to get the other parameters of the visitor to make a judgment.

The website needs to show the number of online visitors

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.