Global.asa -> show user who are online

mgroyal
mgroyal used Ask the Experts™
on
hi

I foud a code in this forum for showing the webuser who are online. The code works fine but I have the problem that the users are not deleted when the session is finish. There must be a failure in the global.asa which I don't know how to fix. Can sombodey help me? I added the call statement before the code because I retrieved always parens error because sub-file... Is perhaps there the mistake?

Here my code: *thx in advance mgroyal

(global.asa)

<SCRIPT LANGUAGE='VBScript' RUNAT='Server'>

Sub Application_OnStart
 Application("userNames") = ""
 Application("userNumber") = 0
End Sub

Sub Session_OnStart
 'End Session after 10 min.
 Session.Timeout = 10
End Sub

Sub Session_OnEnd
 Application.Lock
 
 'Delete Username from the list
CurrentUser = session("currentUser")
i = inStr(CurrentUser,";")-1
UserName = left(Session("CurrentUser"),i)
call replace(Application("userNames"),"|" & UserName,"")

 Application("userNumber") = Application("userNumber") - 1
End Sub
</SCRIPT>

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2005

Commented:
The problem is that the Session_OnEnd is notoriously unreliable, so the code above just won't work.

If you had a button that users would use to log off, you could make that work very easily. However, you can't always count on the user to log off.


Fritz the Blank
Commented:
I think the mistake is in:

call replace(Application("userNames"),"|" & UserName,"")

Replace is a function, not a sub, it should be

Application("userNames") = Replace(Application("userNames"),"|" & UserName,"")
Top Expert 2005

Commented:
Good catch, but it still won't work reliably if the code is being invoked by the Session_OnEnd

Fritz the Blank
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Commented:
I am not sure about the unreliability of the Session_OnEnd.
One must be carefull with using it.

For example, not use any self-defined function that is not defined in the global.asa (which is a mistake easily made), Not use any variable that is not explicitely known in the global.asa.

The point is also that is it hard to debug: no browser displays an error if there is an error in the code.

Commented:
and: I have made several times code working with the Session_OnEnd, such as a "who-is-online" functionality.

Author

Commented:
okay but if if put this code like this:

Application("userNames") = Replace(Application("userNames"),"|" & UserName,"")

then I'll get always the parens-error (parens not accepted in sub or something)

What would you suggest?

Fritz, I belive thats not so reilable but now it does not work at all... There must be something else.

Commented:
Do you have some self-defined Sub called Replace() in your code ?? I guess that must be it...

Author

Commented:
Sorry,

This works...

Application("userNames") = Replace(Application("userNames"),"|" & UserName,"")

but user still not removed

Author

Commented:
Sorry,

This works...

Application("userNames") = Replace(Application("userNames"),"|" & UserName,"")

but user still not removed

Commented:
Application("userNames") is probably a string like this:

"user_1|user_2|user_3|user_4"

When the user you remove is the first in the row, he won't be removed, because he does not have a "|" before his name.

I think it is better to use an array of users, then a string. An array just works easier.


Author

Commented:
No, I don't have self-defined subs...

This is the hole global.asa


<SCRIPT LANGUAGE='VBScript' RUNAT='Server'>

Sub Application_OnStart
 Application("userNames") = ""
 Application("userNumber") = 0
End Sub

Sub Session_OnStart
 'End Session after 10 min.
 Session.Timeout = 10
End Sub

Sub Session_OnEnd
 Application.Lock
 
 'Delete Username from the list
CurrentUser = session("currentUser")
i = inStr(CurrentUser,";")-1
UserName = left(Session("CurrentUser"),i)
Application("userNames") = replace(Application("userNames"),"|" & UserName,"")

Application("userNumber") = Application("userNumber") - 1
End Sub
</SCRIPT>

Author

Commented:
No, I don't have self-defined subs...

This is the hole global.asa


<SCRIPT LANGUAGE='VBScript' RUNAT='Server'>

Sub Application_OnStart
 Application("userNames") = ""
 Application("userNumber") = 0
End Sub

Sub Session_OnStart
 'End Session after 10 min.
 Session.Timeout = 10
End Sub

Sub Session_OnEnd
 Application.Lock
 
 'Delete Username from the list
CurrentUser = session("currentUser")
i = inStr(CurrentUser,";")-1
UserName = left(Session("CurrentUser"),i)
Application("userNames") = replace(Application("userNames"),"|" & UserName,"")

Application("userNumber") = Application("userNumber") - 1
End Sub
</SCRIPT>

Author

Commented:
No, I don't have self-defined subs...

This is the hole global.asa


<SCRIPT LANGUAGE='VBScript' RUNAT='Server'>

Sub Application_OnStart
 Application("userNames") = ""
 Application("userNumber") = 0
End Sub

Sub Session_OnStart
 'End Session after 10 min.
 Session.Timeout = 10
End Sub

Sub Session_OnEnd
 Application.Lock
 
 'Delete Username from the list
CurrentUser = session("currentUser")
i = inStr(CurrentUser,";")-1
UserName = left(Session("CurrentUser"),i)
Application("userNames") = replace(Application("userNames"),"|" & UserName,"")

Application("userNumber") = Application("userNumber") - 1
End Sub
</SCRIPT>

Author

Commented:
sorry, for the posting... problem with IE

Commented:
if you reload the question, then use the link "reload this question", left top - just under your name. You don't re-post comments then.

What is this line for ??

i = inStr(CurrentUser,";")-1

Do you seperate username with a "|" or with a ";" ??

Author

Commented:
currentUser is a session with many user indication such as UserName; Name; ->n which I made to reduce the amount of  session..

i = inStr(CurrentUser,";")-1
UserName = left(Session("CurrentUser"),i)

This is just filtering the userName form the session

Author

Commented:
currentUser is a session with many user indication such as UserName; Name; ->n which I made to reduce the amount of  session..

i = inStr(CurrentUser,";")-1
UserName = left(Session("CurrentUser"),i)

This is just filtering the userName form the session

Author

Commented:
filter ";"

Author

Commented:
found a error which I have not see
-> own soulution made with javascript

Commented:
In global.asa use:

<SCRIPT LANGUAGE="VBScript" RUNAT="Server">
Sub Application_OnStart
    Application.Lock
    Application("user") = 0
    Application.UnLock
End Sub

'When a user connect add 1 to variable

Sub Session_OnStart
    Application.Lock
    Application("user") = Application("user") + 1
    Application.UnLock
End Sub

'On exit remove 1 to variable.
Sub Session_OnEnd
    Application.Lock
    Application("user") = Application("user") - 1
    Application.UnLock
End Sub

</SCRIPT>

On the page use

<%
response.write Application("user")
%>

Jack The Ripper

Commented:
In global.asa use:

<SCRIPT LANGUAGE="VBScript" RUNAT="Server">
Sub Application_OnStart
    Application.Lock
    Application("user") = 0
    Application.UnLock
End Sub

'When a user connect add 1 to variable

Sub Session_OnStart
    Application.Lock
    Application("user") = Application("user") + 1
    Application.UnLock
End Sub

'On exit remove 1 to variable.
Sub Session_OnEnd
    Application.Lock
    Application("user") = Application("user") - 1
    Application.UnLock
End Sub

</SCRIPT>

On the page use

<%
response.write Application("user")
%>

Jack The Ripper

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial