发信人: nightcat()
整理人: jackyz(2000-03-09 03:04:42), 站内信件
|
Using the Global.asa file to create a counter
Before ASP technolgies came about, the internet was a stateless place, meaning there was no way to track user or application variables throu gh a web site.
Fortunately for web developers, Microsoft has developed into the ASP t echnology, application and session objects. These object are then plac ed in a special file named global.asa that is located in the site's ro ot directory and is available to the entire application.
The events that we are concerned about are: application_onStart and ap plication_onEnd, which are triggered when the application is first sta rted or when it is closed or has ended. All variables located here are application specific. The Session_onStart and Session_onEnd events oc cur when a new user or client comes into the website or leaves the web site. All variables contained here are user or client specific.
So what could you use these events for in your application. The most c ommonly used technique is the hit counter. The counter will track how many new sessions have occurred since the application started.
The first thing we must do is start off the application with the count er set to 0 or any other number you like. We will do this in the appli cation_on_Start event.
Sub Application_onStart
Application("NumberofHits") = 0
End Sub
As a new session is started we can increment the application count by 1, by using the session event.
We will lock the application during this increment so that no other se ssions will increment it.
Sub Session_onStart
Application.Lock
Application("NumberofHits") = Application("NumberofHits") + 1
Application.Unlock
End Sub
To view the hit counter just add this line of code somewhere on the ho me page, usually default.asp or index.asp.
You are visitor #<% = Application("NumberofHits")%>
Another area you may use this is to get a count of active sessions on your server at one time. The code is very similar to that above. Add t his line of code to the Application_onStart event.
Application("Numberon") = 0
And to increment the number by 1 when a new session starts, add this c ode in the session_onStart.
Application("Numberon") = Application("Numberon") + 1
The final piece of code lets the server know that a session has ended and therefor is no longer active. Thus, the number of active sessions needs to decrease by one. This code needs to go into the session_onEnd .
Sub Session_OnEnd
Application.Lock
Application("Numberon") = Application("Numberon") - 1
Application.Unlock
End Sub
Finally to output the number on your site enter this line of code.
<%=Application("Numberon")%>
-- ※ 来源:.月光软件站 http://www.moon-soft.com.[FROM: 202.103.124.15]
|
|