Forums

ASP

This topic is locked

displaying who is currently logged in?

Posted 29 Apr 2007 18:16:41
1
has voted
29 Apr 2007 18:16:41 Oli Leach posted:
I am creating a website you have to log in to - the first page is the login page

any way to display who is currently logged in ? I am using the session variable to display the name of who you are logged in as but is there a way to display anyone else who might be logged in at the same time?

Ta

Replies

Replied 29 Apr 2007 22:42:18
29 Apr 2007 22:42:18 Kenneth Halley replied:
I don't work with ASP but my suggestion would be to log your session variables into the database instead of to a temp file- you can do this in php so must be possible to do the same in ASP. Then on your pages simply query the current sessions list.

Another way would be- when registering your session you simply insert the users name into a table set up for the purpose, and you again query this where required. the downside to this method I can think of though is that in doing this should the user disconnect without going through your session clear up routine- ie they simply go straight to another web site without logging out- would still show as being logged in as the db is not intrinsically tied to the session.
Again there would be ways to could get round this with adding timestamps and doing calcs to ensure any timestamps older than a certain length were deleted or flushed but I think the easiest approach is to put sessions into the databse in the first place.
As to how to do it- do a search on the web for database session handling in ASP I am sure you will find something relevant.

HTH
Kenny

-----------------------------------
www.halleynet.co.uk
Replied 29 Apr 2007 23:31:03
29 Apr 2007 23:31:03 Oli Leach replied:
yeah I was thinking along the same lines however I was a bit stumped to know when the user had actually logged out and how I could get the db updated...

I suppose I could query the table to display a recordset of users if they have logged in in the last 10 minutes, in conjunction with updating the record set with a time stamp everytime the user moved around the site to a new page... in other words if the user hasn't updated the recordset by going to another page and say moved onto another website then other people will only see the username for a max 10 minutes. I could even make it less

but it is a workaround which isn't an exact solution - appart from this is there any other way?

thanks Kenneth
Replied 30 Apr 2007 11:14:19
30 Apr 2007 11:14:19 Dave Clarke replied:
you could use a global.asa file and set up various application variables which can then be displayed on a page.

here is a sample global.asa

<pre id=code><font face=courier size=2 id=code> &lt;Object Runat="Server" Scope="application" ID="dOnlineUsers" ProgID="Scripting.Dictionary"&gt;&lt;/Object&gt;
&lt;SCRIPT LANGUAGE="VBScript" RUNAT="Server"&gt;

Sub Application_OnStart
' Set our user count to 0 when we start the server
Application("ActiveUsers" = 0
End Sub
function Decrypt(sText)
'Replace with your own decrypt <img src=../images/dmxzone/forum/icon_smile_wink.gif border=0 align=middle>
Decrypt = sText
end function

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
' Change Session Timeout to 20 minutes (if you need to)
Session.Timeout = 20
' Set a Session Start Time
' This is only important to assure we start a session
Session("Start" = Now
sNewUserName = Decrypt(request.cookies("reuniteuser")

'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 & "&lt;|&gt;" & sLastActionTime & "&lt;|&gt;" & 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

' Increase the active visitors count when we start the session
Application.Lock
Application("ActiveUsers" = Application("ActiveUsers" + 1
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" &lt;&gt; Date() then
application("PageViewsToday" = 0
application("VisitorsToday" = 0
end if
application("VisitorsToday" = application("VisitorsToday" + 1
'Store the date
application("TodaysDate" = Date()

Application.UnLock
End Sub

Sub Session_OnEnd
dOnlineUsers.Remove Session.SessionID
' Decrease the active visitors count when the session ends.
Application.Lock
Application("ActiveUsers" = Application("ActiveUsers" - 1
Application.UnLock
End Sub

&lt;/SCRIPT&gt;</font id=code></pre id=code>

when a new session starts this checks to see if they have a cookie -reuniteuser - which holds their username,if so then it sets <b>sNewUserName</b> to their username or if not it sets it to "guest", it then sets <b>sUserInfo</b> with their name, the time and the last page they were on... these variables can then be displayed on your page.

it also sets up a few application variables <b>Application("ActiveUsers", application("Visitors", application("PageViewsToday" and application("VisitorsToday"</b> which can also be displayed where you want them.

hope this helps

DW8.02|ASP|VBScript|IIS5.1|Access|WinXPPro & WinXPHome.
www.reunite.co.uk
www.dogworld-uk.com

Edited by - Davecl on 30 Apr 2007 11:28:01
Replied 30 Apr 2007 22:53:12
30 Apr 2007 22:53:12 Oli Leach replied:
thanks - i'll have a look at the code you sent and see if i can dicpher it! not a pro coder so see how i get on....

many thanks

Reply to this topic