Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 504
  • Last Modified:

Who is ONLINE

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
alt_shift
Asked:
alt_shift
1 Solution
 
alt_shiftAuthor Commented:
Please help!
0
 
jsmckenzieCommented:
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
 
Neil ThompsonSenior Systems DeveloperCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now