Site needs to display the number of online access

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Insus.NET development of such multi site, customers have no such requirement. However, there is now the customer such requirements.

The online user access number, that is to say, to write a counter for the site, the initial counter value is 0, the site began operation (Application_Start), began to statistics, when a user access (Session_Start) plus 1 counter, when users access to leave (Session_End) counter 1.

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

When the program is started, the definition of a counter, the initial value is 0

 Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs on application startup
        
        Application("OnlineVisitors") = 0
    End Sub

When a user access to web sites:

Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs when a new session is started
        
        Application.Lock()
        Application("OnlineVisitors") = DirectCast(Application("OnlineVisitors"), Integer) + 1
        Application.UnLock()
    End Sub

When the user leaves the site:

Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
        ' 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 SQLServer, the event is not raised.
        
        Application.Lock()
        Application("OnlineVisitors") = DirectCast(Application("OnlineVisitors"), Integer) - 1
        Application.UnLock()
    End Sub

The above two Session_Start and Session_End method, Insus.NET using Application.Lock and Application.Unlock methods, in order to prevent multiple threads change the variables at the same time, changes in the counter, to put it Lock up, change is completed, then the Unlock.

The Global.asax file is stored in the webpage, needs to display a website online access number location:

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

Test, test process, Insus.NET has used two browser, this is to let the web site for the different processes of visitors. Each browser different window open, the access to data.


 Postscript:

This method, just for show, online real statistics can not access number. If you need to really realize the online statistics, still have other parameters need to get visitors to judge.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by William at November 12, 2013 - 12:17 PM