Solved

Who is ONLINE

Posted on 2003-11-17
3
495 Views
Last Modified: 2008-02-01
Hello,
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.
0
Comment
Question by:alt_shift
3 Comments
 

Author Comment

by:alt_shift
ID: 9762768
Please help!
0
 
LVL 2

Expert Comment

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

http://www.codekit.com/source/programming/asp/asp_user_tracking.asp

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.
0
 
LVL 3

Accepted Solution

by:
NeilT earned 100 total points
ID: 9765943
here you go

GLOBAL.ASA

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

<SCRIPT LANGUAGE=VBScript RUNAT=Server>

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")
      '=request.cookies("devUserName")
   
    '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
    application.lock
    '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
    application.unlock
   
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.lock
    application("ActiveUsers") = application("ActiveUsers") - 1
    application.unlock


End Sub
</SCRIPT>

----------------------------------------------------------------------------------------

INCVISITORS.ASP

<%
' 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
            Application.Lock
            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.UnLock
        Else
            Application.Lock
            Application("ActiveUsers") = CInt(Application("ActiveUsers")) + 1
            Application("UserList") = Application("UserList") & Session.SessionID & ":" & Now() & "|"
            Application.UnLock
        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.Lock
    Application("ActiveUsersLastCleanup") = Now()
    Application.Unlock
      
    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"
    Else
        intUsers = intUsers + 1
    End If
Next

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

    Application.Lock
    Application("UserList") = strUserList
    Application("ActiveUsers") = intUsers
    Application.UnLock

End If

End Sub


' Shows the amount of users surfing the site

Call LogNewUser()
Call ActiveUserCleanup()

Response.Write Application("ActiveUsers")


' #########################################################################
' USER TRACKER

    application.lock
    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()
    application.unlock
   
    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" -->



Neil


0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 information …
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

15 Experts available now in Live!

Get 1:1 Help Now