Online number statistics

xiaoxiao2021-03-06  65

In the first two days, some netizens asked this question, I came here to answer.

Logically, this problem is simple. That is to come to a user, the number of online people plus one, leaving a user, the number of online people will be reduced.

The question now needs to solve is how to know the user's entry and increase.

In ASP, when the user enters the access page, it triggers a thing. The event name is session_onstart, when leaving (accurately said that the page data is not accessed within a certain period of time), the default time is 20 minutes), An event will also trigger an event, the name of the event is session_onend. We can define the operations performed by these two events in the global.asa file.

The problem we need to consider is how to make this variable are the same for all people (nonsense, not the same thing that must not be accurate). Therefore, what we first think of is to put online people in an Application variable.

Here, the problems we need to solve have been solved, let's take a look at the code:

SUB session_onstart

Application.lock

Application ("OnlineUsers") = Application ("Onlineusers") 1

Application.unlock

End Sub

SUB session_onend

Application.lock

Application ("Onlineusers") = Application ("OnlineUsers") - 1

Application.unlock

End Sub

There is a small problem here, that is, if the user is the first visitor, the error prompt will occur because the online number variable is not initialized. To solve this problem, we need to add the code initialized the variable:

IF ISempty (Application ("OnlineUsers")).

Application ("OnlineUsers") = 1

END IF

The modified code is as follows:

SUB session_onstart

Application.lock

IF ISempty (Application ("OnlineUsers")).

Application ("OnlineUsers") = 1

Else

Application ("OnlineUsers") = Application ("Onlineusers") 1

END IF

Application.unlock

End Sub

SUB session_onend

Application.lock

Application ("Onlineusers") = Application ("OnlineUsers") - 1

Application.unlock

End Sub

Save the above code as Global.asa, save it in the root directory of the web application (must be in the root directory), then use <% = Application ("OnlineUsers")%> in the appropriate location of the page.

转载请注明原文地址:https://www.9cbs.com/read-119219.html

New Post(0)