Who’s online on your Site

The are many tutorials on the Internet that will show how many active users are
on a website. Here is one – http://www.4guysfromrolla.com/webtech/061399-2.shtml
. But, it can be a bit trickier to extend this to give a list of the active users.
In the example I going to show you, I will store the users in an XML file. This
method may not be perfect, but may suit the needs of many.

After a user logs in, we can store their details in XML. The file below can
be included in each page. All the demo files can be downloaded from link at
the bottom of the page. When a page is loaded the following actions are performed:

The XML is loaded from disk (the folder containing the XML file must have
the correct permissions to write to it)
2. Users are removed from the file that haven’t visited a page in the last
5 minutes (this is stored in the UserTimeout variable and can be changed)
3. Any reference to the current user is also removed.
4. A timestamp is added for the current user.
5. The XML file is then saved to disk.

Another include file is then used to show the users. This is included in the
demo files below.

'This file goes on every page that is is protected
UserTimeout=5 'A 5 minute timeout seems reasonable

Set objXML = Server.CreateObject("Microsoft.XMLDOM")
set objNodeXML=objXML.documentElement
objXML.Save XmlFile
Set objXML = Nothing

function RemoveOldNodes
	set OldNodes=objNodeXML.selectNodes("user[@date < '" & ExpiryDate & "']")
	if IsObject(OldNodes) then
		for each nd in OldNodes
	end if
end function

function RemoveUserNodes
	set CurrentUserNodes=objNodeXML.selectNodes("user[@username='" & Username & "']")
	if IsObject(CurrentUserNodes) then
		for each nd in CurrentUserNodes
	end if
end function

function AddUserNode
	set newNode=objXML.createNode(1,"user","")
	newNode.setAttribute "username",Username
	newNode.setAttribute "date",IsoFormat(now)
end function

function IsoFormat(t)
	IsoFormat=year(t) & FormatDT(month(t)) & FormatDT(day(t)) & FormatDT(Hour(t)) & FormatDT(minute(t)) & FormatDT(second(t))
end function

function FormatDT(o)
	if len(o)=1 then
		FormatDT="0" & o
	end if
end function
Updated: June 30, 2020 — 9:16 pm