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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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

Neil Thompson 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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

624 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