Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop Through Session Variables For All Session IDs?

Status
Not open for further replies.

lbarron

Technical User
Jan 22, 2002
92
GB
Hi,

I have a session variable that is set when a user logs into our ASP application. What I would like to do is list all instances of this session variable.

Is it possible to cycle through each session id and get the value of a particular session variable?

Cheers

Lee
 
you mean you want to get all the currently logged in users??

is that right??

-DNG
 
this is how you do it...

Code:
Dim strName, iLoop
'Use a For Each ... Next to loop through the entire collection
For Each strName in Session.Contents
  Response.Write strName & " - " & Session.Contents(strName) & "<BR>"
Next

-DNG
 
DNG,

Yes, that is what I would like to do.

Unfortunately, I have used the code above but it only seems to show the variables from a single session. For example, if I log into the system and then ask my colleague to login I can only see the variables associated with MY session, ideally I would like to be able to cycle through all session id's.

Hope this clears things up.

Thanks

Lee
 
Hi,

Thanks for all your replies.

Still not sorted this one out!, any other suggestions!?!

Surely, there must be a way of seeing session variables assigned to other sessions other than your own?

Cheers

Lee
 
Lee,

I don't think that it is possible (in the way you might want) to access multiple session objects within the ASP environment. For security's sake, the connected user is bound to the session they use through the SESSIONID generated when they first access the application, which is stored in a temporary session cookie on the users computer.

Although I may be mistaken, I do not believe there to be a collection for session objects (a collection above the built in Session object) - so there is no way to iterate through all the sessions currently active. (you can iterate through the session.contents collection, but that is not what you want)

So we cannot do this for example:

Application.Sessions(Session.SessionID).LCID
or
Server.Sessions(Session.SessionID).Abandon

Depending on what you are trying to actually do, there are alternative designs and methods that could help you.

For just one variable you could set both the session variable and an application variable with the same value (but the application variable would hold an array rather than a single value)

( Here's how to manage arrays in application & session variables.. )

Or you can add it to a string if you want it really simple:

application("something") = "Value1|Value2|Value3"
myarray = split(application("something"),"|")
(add session id / user id if you need to know which session / user the value belongs to)

This will then give you access to the variable across all users. But for more than one variable, or long/complex information this might not be feasible. If that is the case then I would say you should try using a database to store the session details in.. that way you have full access and control over all sessions.

If you still must access the sessions directly then you may be able to do it using WMI - though how much info you could get from this I'm not sure, and it would require that you logged onto the web application as an administrator.

Here is a link which includes code for some of these examples - in particular the WMI bit - you may be able to extend this to analyse the contents of the session.. maybe not (a bit of a long shot).


I think your best option if you have alot of information to share is the DB method. Though quick and dirty would be the application variable string that you can split into an array - used with the SessionID and the Session_OnEnd event, you should be able to keep this reasonably in check - though be warned that this is not the most reliable method!

There may be some way to directly access other sessions' variables, but it seems unlikely. - If someone else knows, it would be interesting to see.

Hope this helps,

A smile is worth a thousand kind words. So smile, it's easy! :)
 
Hi,

Thanks for your replies.

FesterSXS - Yes I would like to read multiple instances of a session variable, not just one session.

Damber - Maybe the application variable or database storage is the way to go!

Thanks for all your help.

cheers

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top