• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Detecting when a user leaves a site

I need to call some server side code when a user leaves my site to delete a cookie. Unfortunately, I can't just make it a session cookie, for various reasons. I realize there's an onunload event for the body tag, but this event is fired for refreshes, postbacks, internal links, etc. I need a way to check where the user is going (or if they're closing their browser).
 
I don't think there is really a way to do that even if you have it set to expire right away. You could always forcefully close the browser and the cookie, the session, etc. will all be there idle.
 
If they leave the site (like closing the tab or using a bookmark) you can't very well delete the cookie because the browser hopefully won't be listening for anything else from you. You might get something that works most of the time but, like exdeath said, you'll never do it properly so you're far better off just designing something that works well with timeouts.

Why can't you use a cookie that expires at the end of the session? (That being distinct from the usual meaning of 'session cookie' which is something specifically used to identify session state in a webapp).
 
Web apps don't work like normal apps, there's no reliable way to tell when the user's Internet connection dies, they kill the browser, etc.
 
You can probably do an asynchronous XMLHttpRequest that never terminates, and watch for the connection to be closed by the browser...

I'm not sure how you plan on deleting a cookie with server-side code though.
 
Originally posted by: CTho9305
You can probably do an asynchronous XMLHttpRequest that never terminates, and watch for the connection to be closed by the browser...

I'm not sure how you plan on deleting a cookie with server-side code though.

It's complicated. This is a forms auth ticket generated by a custom .NET membership provider. It's used in a custom SSO with a 3rd party web app, all tied back in to SharePoint 2007. In order to make Office client integration work with forms auth, we have to make the auth ticket persistent. The 3rd party web app uses it's own session cookie, so that after the session expires or the user closes the browser, they are still logged in to our site, but not to the 3rd party web app.
 
Back
Top