Tracking Current Web Sessions
-
Hey there, I'm working on a webpage using ASP and C#. At the server, I am tracking all of the live Sessions based on the HttpContext.Current.Session.SessionID value. As you probably know this is a value unique to each web browser accessing my pages. Now my problem. I would like to know when a user closes the browser and leaves the site (assuming he/she doesn't "log out"). I am hoping to periodically check each SessionID to see if it is still current or expired. Is there a way to load a specific session based on its ID and check to see if it has expired or not? If it matters, I am using cookies to store the session information. Thanks in advance, Pualee -- modified at 11:10 Friday 3rd February, 2006 P.S. -- Im reading a few books and looking all over the web, but having difficulty finding good information about Sessions (I'm new to Web Development). If you know a good online faq or tutorial, please share the love/url here ;P
-
Hey there, I'm working on a webpage using ASP and C#. At the server, I am tracking all of the live Sessions based on the HttpContext.Current.Session.SessionID value. As you probably know this is a value unique to each web browser accessing my pages. Now my problem. I would like to know when a user closes the browser and leaves the site (assuming he/she doesn't "log out"). I am hoping to periodically check each SessionID to see if it is still current or expired. Is there a way to load a specific session based on its ID and check to see if it has expired or not? If it matters, I am using cookies to store the session information. Thanks in advance, Pualee -- modified at 11:10 Friday 3rd February, 2006 P.S. -- Im reading a few books and looking all over the web, but having difficulty finding good information about Sessions (I'm new to Web Development). If you know a good online faq or tutorial, please share the love/url here ;P
Unless you use javascript in the browser to tell the server when the user leaves the page, there is no way that the server can know when this happens. If the user doesn't log out, the server session ends 20 minutes (at default setting) after the last request. Any information you store in a cookie is only available when a request is made, as the cookie is stored in the browser. --- b { font-weight: normal; }
-
Unless you use javascript in the browser to tell the server when the user leaves the page, there is no way that the server can know when this happens. If the user doesn't log out, the server session ends 20 minutes (at default setting) after the last request. Any information you store in a cookie is only available when a request is made, as the cookie is stored in the browser. --- b { font-weight: normal; }
Guffa wrote:
Unless you use javascript in the browser to tell the server when the user leaves the page, there is no way that the server can know when this happens.
I'm not so much worried about the "event" of leaving, I just want to clean up some things periodically... While still researching this topic, I have come across the following: System.Web.SessionState.HttpSessionState class .Count - Gets the number of items in the session-state collection .Keys - Gets a collection of keys for all the values in the session-state collection Shouldn't it be possible to loop on the .Keys to compare the active Keys against the keys my application believes are active? This might not be very scalable, but I'm not expecting thousands of users to connect per hour, this is a very specialized application. I'm going to try this for now, but I'll be rechecking this board for feedback (never know when someone else has the same problem). Also, would keeping the session data on server side, rather than as cookies, make this possible? Pualee
-
Guffa wrote:
Unless you use javascript in the browser to tell the server when the user leaves the page, there is no way that the server can know when this happens.
I'm not so much worried about the "event" of leaving, I just want to clean up some things periodically... While still researching this topic, I have come across the following: System.Web.SessionState.HttpSessionState class .Count - Gets the number of items in the session-state collection .Keys - Gets a collection of keys for all the values in the session-state collection Shouldn't it be possible to loop on the .Keys to compare the active Keys against the keys my application believes are active? This might not be very scalable, but I'm not expecting thousands of users to connect per hour, this is a very specialized application. I'm going to try this for now, but I'll be rechecking this board for feedback (never know when someone else has the same problem). Also, would keeping the session data on server side, rather than as cookies, make this possible? Pualee
You may want to consider setting a short (< 5 mins.) session timeout on your app and have every page periodically (using JavaScript) retrieve a very small resource (eg: a 1x1 pixel transparent
.gif
) in aniframe
. /ravi My new year's resolution: 2048 x 1536 Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com -
Guffa wrote:
Unless you use javascript in the browser to tell the server when the user leaves the page, there is no way that the server can know when this happens.
I'm not so much worried about the "event" of leaving, I just want to clean up some things periodically... While still researching this topic, I have come across the following: System.Web.SessionState.HttpSessionState class .Count - Gets the number of items in the session-state collection .Keys - Gets a collection of keys for all the values in the session-state collection Shouldn't it be possible to loop on the .Keys to compare the active Keys against the keys my application believes are active? This might not be very scalable, but I'm not expecting thousands of users to connect per hour, this is a very specialized application. I'm going to try this for now, but I'll be rechecking this board for feedback (never know when someone else has the same problem). Also, would keeping the session data on server side, rather than as cookies, make this possible? Pualee
Pualee wrote:
I just want to clean up some things periodically...
Then use the Session_End event in global.asax.
Pualee wrote:
Shouldn't it be possible to loop on the .Keys to compare the active Keys against the keys my application believes are active?
You are confusing session objects with session variables. Each user has one session object that each can contain several variables. The Session.Keys property contains the keys of the variables that has been stored in the session object for the current user. The server of course keeps track of the active sessions, but it doesn't expose this list to anyone. Probably because items can be removed from the list at any time.
Pualee wrote:
Also, would keeping the session data on server side, rather than as cookies, make this possible?
That depends. What is it that you store in the cookies anyway? --- b { font-weight: normal; }