Posted on 2003-11-17
Last Modified: 2008-02-01
I want to know who is online. I dont want to user db. And I cant really detect when a session ens if user doesnt click LOG OFF. How can I detect inactivity of a user. And can I do this operation with GLOBAL.ASA and Applications object. Thanks in advance.
Question by:alt_shift

Author Comment

ID: 9762768
Please help!

Expert Comment

ID: 9764802
Did a search in Google for "user application global.asa" and found this document:

If a user doesn't explicity log off, the only way to tell the user is inactive is if the ASP session times out.  If you wanted to track individual users, you could create another Application variable as an array that contains the user names and other info.

Accepted Solution

NeilT earned 100 total points
ID: 9765943
here you go


<Object Runat="Server" Scope="application" ID="dOnlineUsers" ProgID="Scripting.Dictionary"></Object>


sub Application_OnStart
    application("ServerStart") = now
end sub

function adZero(sText)
    'Adds zeros to the beginning
    if isNull(sText) then exit function
    adZero = string(5 - len(sText), "0") & sText
end function

Sub Session_OnStart

    'Session_OnStart is automatically called by the ASP Interpreter
    'when a new session starts. (Probably a new visitor)

    'TimeOut value determines the period in minutes when
    'the session is assumed to be abandoned

    Session.TimeOut = 20

    'Get the current active users list
    sActiveUsersList = application("ActiveUsersList")
    'Get the new user name from the cookie
    sNewUserName = Session("logged_in_status")
    'If the visitor doesn't have a registered username, assign Guest(n) label
    if sNewUserName = "" then sNewUserName = "Guest-" & AdZero(CInt(application("Visitors")))
    'Initial action time
    sLastActionTime = Time
    'User info consists of user name, last action time, and the page viewed
    sUserInfo = sNewUserName & "<|>" & sLastActionTime & "<|>" & sLastPageViewed

    'Add this user to our collection with SessionID being the key
    '(See the top of this file to see how dOnlineUsers object is initiated)
    dOnlineUsers.Add Session.SessionID, sUserInfo
    'Lock the application variable and update the contents
    'The number of active users
    application("ActiveUsers") = application("ActiveUsers") + 1
    'The number of visitors since last application reset
    application("Visitors") = application("Visitors") + 1
    'If the date is different than the previously stored date,
    'it means we passed midnight, so reset our "today" counters
    if application("TodaysDate") <> Date() then
        application("PageViewsToday") = 0
        application("VisitorsToday") = 0
    end if
    application("VisitorsToday") = application("VisitorsToday") + 1
    'Store the date
    application("TodaysDate") = Date()
    'Unlock and go
End Sub

Sub Session_OnEnd
    'Session_OnEnd is automatically called by the ASP Interpreter
    'when the specified TimeOut period has passed after user's last action

    on error resume next

    'Remove this session from our collection
    dOnlineUsers.Remove Session.SessionID
    'And update the application variables
    application("ActiveUsers") = application("ActiveUsers") - 1

End Sub



' Logs the active user so that the rest of the script can know the active users....
Sub LogNewUser
            Dim strUserList
            Dim intUserStart, intUserEnd
            Dim strUser
            Dim strDate

            strUserList = Application("UserList")

        If Instr(1, strUserList, Session.SessionID) > 0 Then
            intUserStart = Instr(1, strUserList, Session.SessionID)
            intUserEnd = Instr(intUserStart, strUserList, "|")
            strUser = Mid(strUserList, intUserStart, intUserEnd - intUserStart)
            strUserList = Replace(strUserList, strUser, Session.SessionID & ":" & Now())
            Application("UserList") = strUserList
            Application("ActiveUsers") = CInt(Application("ActiveUsers")) + 1
            Application("UserList") = Application("UserList") & Session.SessionID & ":" & Now() & "|"
        End If
End Sub

'Cleans up the user count so that the script can read the user details from it...

Sub ActiveUserCleanup
Dim ix
Dim intUsers
Dim strUserList
Dim aActiveUsers
Dim intActiveUserCleanupTime
Dim intActiveUserTimeout

intActiveUserCleanupTime = 2 'In minutes, how often should the UserList be cleaned up.
intActiveUserTimeout = 20  'In minutes, how long before a User is considered Inactive and is deleted from UserList

If Application("UserList") = "" Then Exit Sub

If DateDiff("n", Application("ActiveUsersLastCleanup"), Now()) > intActiveUserCleanupTime Then

    Application("ActiveUsersLastCleanup") = Now()
    intUsers = 0
    strUserList = Application("UserList")
    strUserList = Left(strUserList, Len(strUserList) - 1)

    aActiveUsers = Split(strUserList, "|")

For ix = 0 To UBound(aActiveUsers)
    If DateDiff("n", Mid(aActiveUsers(ix), Instr(1, aActiveUsers(ix), ":") + 1, Len(aActiveUsers(ix))), Now()) > intActiveUserTimeout Then
        aActiveUsers(ix) = "XXXX"
        intUsers = intUsers + 1
    End If

strUserList = Join(aActiveUsers, "|") & "|"
strUserList = Replace(strUserList, "XXXX|", "")

    Application("UserList") = strUserList
    Application("ActiveUsers") = intUsers

End If

End Sub

' Shows the amount of users surfing the site

Call LogNewUser()
Call ActiveUserCleanup()

Response.Write Application("ActiveUsers")

' #########################################################################

    application("PageViews") = application("PageViews") + 1
    if application("TodaysDate") <> Date() then
        application("PageViewsToday") = 0
        application("VisitorsToday") = 0
    end if
    application("PageViewsToday") = application("PageViewsToday") + 1
    application("TodaysDate") = Date()
    if session("devUserName") = "" then
        session("devUserName") = Session("logged_in_status")
        session("devFullName") = Session("logged_in_status")
    end if

    session("PageViewed2") = session("PageViewed1")
    session("PageViewed1") = session("PageViewed0")
    session("PageViewed0") = request.ServerVariables("script_name")
    sUserInfo = dOnlineUsers.Item(Session.SessionID)
    aUserInfo = split(sUserInfo, "<|>")
    aUserInfo(1) = Time()
    aUserInfo(2) = request.ServerVariables("script_name")
    sUserInfo = aUserInfo(0) & "<|>" & aUserInfo(1) & "<|>" & aUserInfo(2)
    dOnlineUsers.Item(Session.SessionID) = sUserInfo
      response.write "<br>" & application("VisitorsToday") & " visitors have viewed " & application("PageViewsToday") & " pages from this site TODAY!"



then just include the incvisitors.asp file where you want the cound displayed

<!--#include file="incvisitors.asp" -->



Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASP VB... 7 99
An ASP query I cannot work out... 2 46
JQuery Date Time picker not showing 29 113
Error viewing ASP page 12 149
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: and…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now