ASP: Active Sessions, Active Logins and Total Site Hits - Active Logins
(Page 4 of 5 )
After reading the information stated on the previous pages in this article and understanding the concept of Application and Session objects, I will dwell into keeping track of active Logins on your site. This is slightly trickier as we have to take into account that not every user that surfs your site will Login and not every Login user will manually terminate their Login session by logging out.
To achieve the above objectives, we will make use of two flag variables called Application("aflgLogin") and Session("sflgLogin") that keeps track of whether the user has logged out manually or the session terminated naturally.
If you have a Login page running on ASP, chances are that you will be verifying the username and password passed with the values stored in a database. Once that is verified, you would most likely assign Session variables to the Login user to keep track of his username, etc. All we need to add after the user is verified is to add these 2 lines of code once the user is verified.
'ON THE LOGIN PAGE
Session("sflgLogin") = True 'Session Flag Variable
Application("aLogins") = Application("aLogins") + 1
From there, we know the number of Active Logins by retrieving the value of Application("aLogins") variable and write it to an ASP page.
On the logout page if the user wants to manually logout, we implement these lines behind the ASP Logout page
'ON THE LOGOUT PAGE
If Session("sblnLogin") = True then 'Utilize the Session Flag Variable
'Decrement the Active Logins accordingly
Application("aLogins") = Application("aLogins") - 1
'For Safety measures that the Active Logins cannot fall below ZERO
If Application("aLogins") <= 0 then
Application("aLogins") = 0
End If
Session("sblnLogin") = False
Application.Lock
'A application Flag variable to be passed to Session_OnEnd when 'Session.Abandon is called next
'Reason why we pass to an Application Flag Variable is because the 'Session.Abandon method clears all Session variables
'and runs the Session_OnEnd event of the GLOBAL.ASA file.
'We need to keep track of whether the user has logged out manually or if the 'session has timed out naturally and we
'therefore need to keep track of this Application Flag Variable.
Application("ablnLogin") = False
Application.Unlock
End If
Session.Abandon
On the GLOBAL.ASA file, we need to add these lines in the following events
Sub Session_OnStart
'In case this Application variable to keep track of active Logins doesnt exists, 'initialize it to 0
If IsEmpty(Application("aLogins")) Then
Application("aLogins") = 0
End If
End Sub
Sub Session_OnEnd
Application.Lock
'Application("aflgLogin") = False means User has Destroyed Session, therefore 'reset Application("aflgLogin") then Exit Sub
'No need to Decrement Application("aLogins") anymore as it had been done so 'at LogOut ASP Page
If Application("aflgLogin") = False then
Application("aflgLogin") = ""
Application.Unlock
Exit Sub
End If
'If Program flowed into here, it means that Application("aflgLogin") is not False 'which means that the session
'terminated naturally, therefore Decrement Application("aLogins") here.
Application("aLogins") = Application("aLogins") - 1
'For Safety measures that the Active Logins cannot fall below ZERO
If Application("aLogins") <= 0 then
Application("aiLogins") = 0
End If
Application.Unlock
End Sub
Next: Conclusion >>
More Apache Articles
More By Softwaremaker