Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Who is ONLINE

Posted on 2003-11-17
3
Medium Priority
?
503 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
[X]
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
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:
Neil Thompson earned 400 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

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 was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

730 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