locked
Terminate Session On Browser Closing RRS feed

  • Question

  • User-817646753 posted

    just for the record...

    ive seen a lot of threads on this forum explaining how you cant detect the browser closing client-side so you have to wait for the session_end to fire.

    calling something like this from the onbeforeunload event handler of the <BODY> tag will do the job:

    function MyFunction()
    {
        if (event.clientY < 0)
            window.top.location.href = "<path to logout page>";
    }

    this will fire the code behind in your logout page even though the HttpHandler will not actually display it. but you should still have backup timeout logic in case of power cut etc.

    Thursday, September 29, 2005 10:11 PM

All replies

  • User-504499122 posted

    just for the record...

    ive seen a lot of threads on this forum explaining how you cant detect the browser closing client-side so you have to wait for the session_end to fire.

    calling something like this from the onbeforeunload event handler of the <BODY> tag will do the job:

    function MyFunction()
    {
        if (event.clientY < 0)
            window.top.location.href = "<path to logout page>";
    }



    Onbeforeunload is unreliable.You can not completely depend on that event. It fires in so many scenario's. Lets say you are navigating to a different page, then that event fires,lets say you are opening a popup window, it fires. Lets say you close the window , it fires.. so how is the browser supposed to know what you wanted to do.. How can it differentiate from a simple browser closing to a navigating to another page

    Thursday, September 29, 2005 10:55 PM
  • User-817646753 posted
    it can differentiate by checking the clientY property of the event object. This way, you can detect if the user clicked on the close X button.

    see line:  if (event.clientY < 0)
    Friday, September 30, 2005 4:22 AM
  • User-504499122 posted
    it can differentiate by checking the clientY property of the event object. This way, you can detect if the user clicked on the close X button.

    see line:  if (event.clientY < 0)


    Yeah.. Its a bit of hack.. to check for the position of the cursor.. But think about this.. I right click on the top of the window somewhere and closes it.. Or I use Alt+F4 or I simply turn off my machine without closing window.. As I said, there are so many cases to consider. We tried all these hacks in our projects.. but as I said.. its not that easy
    Friday, September 30, 2005 9:39 AM
  • User-817646753 posted

    kumar - thanks for your feedback.

    of course you are right - this script will not detect things like a machine crash. where it does come in handy is for issues such as tracking user concurrency for licensing purposes. the best way to tackle the concurrency issue ASP.NET is (IMHO) with "last one in wins" logic.

    however if you must implement a concurrency solution and cannot use the last one in wins logic, the only real viable alternative is something like this: http://www.eggheadcafe.com/articles/20030418.asp.

    but in this scenario a user must wait until their cache entry has expired before being allowed to log back in. by using this logic in conjunction with the javascript I have attached to this post, you can at least trap 99% of browser closures and thereby prevent the user from waiting to log back in.

    Friday, September 30, 2005 5:16 PM
  • User1461997835 posted
    Thanks Bubba!  This is exactly what I was looking for!
    Monday, October 3, 2005 12:48 PM
  • User-817646753 posted
    hi dbeach

    please note that you must have backup logic though. although this method will trap 99% of closures it cannot be viewed as 100% safe.

    Monday, October 3, 2005 7:07 PM
  • User1461997835 posted
    Hi Bubba,

    Of course I have the backup logic, but this just turned out to be very helpful in what I am trying to achieve - thanks for the friendly reminder though, you're so nice :-)
    Tuesday, October 4, 2005 12:09 PM
  • User1594187029 posted
    hi,
    i tried to use this code to run my php code(which updates a table) when onbeforeunload event fires.
    my file name is db1.php
     window.top.location.href = http://localhost/db1.php;
    but its not working
    plz help me

    thanks in advance

    Friday, October 28, 2005 8:14 PM
  • User798903548 posted
    This is not a php forum, it's an ASP.NET forum, but try putting the url inside single quotes (').

    NC...
    Saturday, October 29, 2005 11:43 AM
  • User-389939489 posted
    In case you are interestead, there *is* a reliable way to trap a client disconnect.

    It's shortly described here: http://forums.asp.net/1095234/ShowPost.aspx

    The basic idea is implementing in JS the equivalent of a keep-alive logic (but even a meta refresh tag in a hidden iframe could do the trick...).

    Hope This Helps. -LV

    Thursday, November 3, 2005 5:06 PM