ASP: Active Sessions, Active Logins and Total Site Hits - Writing The Code (Page 3 of 5 )
Once you understand the basic concepts of Application and Session objects, you are ready to do wonders with your web application which traditionally takes a lot more coding and programming than your basic standalone ones as the HTTP protocols web applications run on are basically stateless (donít remember anything once they are run).
Letís get our hands dirty by understanding and writing some codes. Because, a Session is instantiated whenever a new browser requests content, a page hit should always be incremented here. To provide for data-persistence when a server shuts down, we will attempt to keep an application count (used throughout the application until the server shuts down) and then write that application count to a text file or a database to keep a "Hard-Copy" record of the total number of site-hits.
Sub Session_OnStart() Dim TotalVisits Application.Lock 'Application("aHitCounter") is the application variable for keeping track of total number 'of site-hits 'Application("aActiveSess") is the application variable for keeping the number of active 'sessions of your site the current moment. 'In case this Application variable to keep track of active sessions doesnt exists, 'initialize it to 0 If IsEmpty(Application("aActiveSess")) Then Application("aActiveSess") = 0 End If 'Read from a stored Text File or a Database that keeps a record of the total no. of site 'hits. '(We wont cover this topic here) and save the result to a variable called 'TotalVisits 'Increment TotalVisits by 1 TotalVisits = Clng(TotalVisits) + 1 'Save this TotalVisits Variable to the Application("aHitCounter") variable 'This application variable can then be used throughout the application, regardless of the 'number of sessions Application("aHitCounter") = TotalVisits 'Write Back to the same stored Text File or Database that keeps a record of the total 'no. of site hits the incremented value of the total number of Hits 'To Increment the Active Session Count Whenever a New Session Starts Application("aActiveSess") = Application("aActiveSess") + 1 Application.UnLock End Sub
After a new user requests any pages from your site, this GLOBAL.ASA file will always run with the this event above. After that, throughout the application, we can access the total number of hits and the active sessions through the Application("aHitCounter") variable and Application("aActiveSess") variable respectively. Now, we have to take care of the Application("aActiveSess") to make sure that this counter is decremented properly when the user session ends or times out. This is done via the Session_OnEnd Event
Sub Session_OnEnd 'Decrement the Active Session Count After Session Ends Application.Lock Application("aActiveSess") = Application("aActiveSess") - 1 Application.UnLock End Sub
Thats all. When the user session ends or times out, the above event will run and this application("aActiveSess") will be decremented accordingly to better reflect the number of ACTIVE sessions on you site.