Advanced Users Online

Many people have tutorials out there that allow you to count the number of active users online with the use of the global ASA file; however, they do not show you the user's location and "what they are doing".

i.e, going to your users_online.asp page may show the following:

  • user J.Buttafucco is viewing the guestbook (/dev/guestbook.asp)
  • user A.Fischer is deleting a guestbook entry (/dev/guestbook_delete.asp)
  • user T.Harding is beating people up (/dev/beat_peeps_up.asp)

The Include File

Ok. Next, we need to include an include (bad grammar, so sue me) EVERY PAGE NEEDS TO HAVE AN INCLUDE REGARDLESS OF WHETHER IT WILL ACCESS A DATABASE OR NOT. We'll call this file myInclude.asp

<%
' FileName="Connection_ado_conn_string.htm"
' Type="ADO"
' HTTP="false"
' Catalog=""
' Schema=""
MM_connection_STRING= "Trusted_Connection=yes; Driver={SQL Server}; Server=servername; Database=databasename; UID=username; PWD=password"
%>

<%
Sub LogActiveUser
' =========================================
' = This sub is called to add the user's initial information
' = to the database. First, insert new record if SessionID
' = does not exist. Insert Session("person"),
' = Session("location"), Request.ServerVariables("SCRIPT_NAME"),
' = and Session("doing") into database
' =========================================

' =========================================
' = check to see if sessionID exists
' =========================================

MM_rsKeyConnection=MM_connection_STRING
MM_dupKeyUsernameValue = CStr(Session.SessionID)
MM_dupKeySQL="SELECT fldSessionID FROM dbo.tblActiveUsers WHERE fldSessionID='" & MM_dupKeyUsernameValue & "'"
MM_adodbRecordset="ADODB.Recordset"
Set MM_rsKey=Server.CreateObject(MM_adodbRecordset)
MM_rsKey.ActiveConnection=MM_rsKeyConnection
MM_rsKey.Source=MM_dupKeySQL
MM_rsKey.CursorType=0
MM_rsKey.CursorLocation=2
MM_rsKey.LockType=3
MM_rsKey.Open
If Not MM_rsKey.EOF Or Not MM_rsKey.BOF Then
' the SessionID was found - can not add the requested SessionID
Else
MM_editQuery = "insert into dbo.tblActiveUsers (fldSessionID, fldUsername, fldLocation, fldDoing) values ('" & Session.SessionID & "', '" & Session("person") & "', '" & Session("location") & "', '" & Session("doing") & "')"
Set MM_editCmd = Server.CreateObject("ADODB.Command")
MM_editCmd.ActiveConnection = MM_connection_String
MM_editCmd.CommandText = MM_editQuery
MM_editCmd.Execute
MM_editCmd.ActiveConnection.Close
End If
MM_rsKey.Close

End Sub

Sub UpdateActiveUser
' =========================================
' = This sub us called each time the user requests a new
' = page. It makes sure that the location and the doing
' = status are correctly set.
' =========================================
MM_editQuery = "update dbo.tblActiveUsers set fldLocation = '" & Session("location") & "', fldDoing = '" & Session("doing") & "' where fldSessionID = '" & Session.SessionID & "'"
MM_editConnection = MM_connection_STRING
Set MM_editCmd = Server.CreateObject("ADODB.Command")
MM_editCmd.ActiveConnection = MM_editConnection
MM_editCmd.CommandText = MM_editQuery
MM_editCmd.Execute
MM_editCmd.ActiveConnection.Close
End Sub

Sub LogOutActiveUser
' =========================================
' = This sub is used to remove an expired user's information
' = from the database. Search via the current SessionID
' = if found, remove it from the database. This should leave
' = it so that only active Sessions are remaining in the
' = database.
' =========================================
MM_editQuery = "delete from dbo.tblActiveUsers where fldSessionID = '" & Session.SessionID & "'"
MM_editConnection = MM_connection_STRING
Set MM_editCmd = Server.CreateObject("ADODB.Command")
MM_editCmd.ActiveConnection = MM_editConnection
MM_editCmd.CommandText = MM_editQuery
MM_editCmd.Execute
MM_editCmd.ActiveConnection.Close
Session.Abandon
End Sub
%>

Comments

Great

July 23, 2002 by scre wdanger

Great tutorial. Just one thing would have make things lot easier for the newbies. I can imagine that volks will have some problems in implementing the script so why not provide them the files and the database. Then things will be lot more easier to understand.

Anyways thanxalot finally i got it workin,  was lookin for this for a long time.

RE: Great

October 19, 2002 by arend rutgers

Great??

I don't even know why its rated 4.7?? It doesnot work. There are many errors. I am working on it if somebody sorted out then please let me know.

 

You must me logged in to write a comment.