Forums

This topic is locked

I am a god. (advanced users)

Posted 14 Jul 2002 08:09:58
1
has voted
14 Jul 2002 08:09:58 aegis kleais posted:
I've devised a way to track active sessions, and report where they are and what they're doing. 8<img src=../images/dmxzone/forum/icon_smile.gif border=0 align=middle> Oh man, this is quite a bit of crap so let me just give you the basics.

=========================================
= You'll need a global.asa with something like the following:
=========================================

&lt;script language="vbscript" runat="server"&gt;

Sub Application_OnStart
Application.Lock
Application("usersOnline" = 0
Application("visits" = 0
Application.UnLock
End Sub

Sub Session_OnStart
Application.Lock
Application("usersOnline" = Application("usersOnline" + 1
Application("visits" = Application("visits" + 1
Application.UnLock
End Sub

Sub Session_OnEnd
Application.Lock
Application("usersOnline" = Application("usersOnline" - 1
Application.UnLock
End Sub

&lt;/script&gt;

=========================================
= And you'll need an include with code like:
=========================================

&lt;%
' FileName="Connection_ado_conn_string.htm"
' Type="ADO"
' HTTP="false"
' Catalog=""
' Schema=""
MM_dbConnection_STRING = "Trusted_Connection=yes; Driver={SQL Server}; Server=servername; Database=databasename; UID=userID; PWD=password"
%&gt;

&lt;%
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
' =========================================
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_dbConnection_String
MM_editCmd.CommandText = MM_editQuery
MM_editCmd.Execute
MM_editCmd.ActiveConnection.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_dbConnection_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_dbConnection_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
%&gt;

On your page right after the login of your user, place:

Call LogActiveUser
Call UpdateActiveUser

On your page that logs out the user with Session.Abandon, remove Session.Abandon and replace it with:

Call UpdateActiveUser
Call LogOutActiveUser

(You'll note that the LogOutActiveUser Sub includes the Session.Abandon AFTER it uses the Session.SessionID to remove that user from the database)

On everypage you'll need this code to set the location and doing

&lt;%
Session("location" = Request.ServerVariables("SCRIPT_NAME"
Session("doing" = "looking at the guestbook"
%&gt;

And that's about it. Here's the follow through on what happens.

A user visits your website and logs in. The SessionID is placed along with the user's name and the location and doing (which are set on each page) into the database. Each time the user requests a new page, the location and doing are the only things updated. When the user leaves, the SessionID is used as a "SEARCH for this record and delete it" and then the Session is Abandoned (which will also lower your OnlineUsers counter when the session actually expires.

TADA!!

Aegis Kleais
New Media Web Developer
(DWMX : IIS5.1 : SQL2K : WXP : ASP[VB/JS])

Replies

Replied 14 Jul 2002 18:37:12
14 Jul 2002 18:37:12 Dave Clarke replied:
yes you are god <img src=../images/dmxzone/forum/icon_smile_wink.gif border=0 align=middle>

thank you for sharing that with us, don't think i'll even attempt to use that, much too complex for my little brain, but well done.
you deserve to be chuffed with yourself.
Replied 15 Jul 2002 03:08:59
15 Jul 2002 03:08:59 b w replied:
Pretty Good!
I would like something like this for my site. Do you want to turn it into a tutorial for udzone?



Replied 16 Jul 2002 00:03:05
16 Jul 2002 00:03:05 Alfa Siete replied:
¡Bravo Maestro!
(no translation needed... I supose)

Everybody should belive in something... I belive i'm having another beer

Reply to this topic