How to Destroy Session On Window Close ???

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • malav123
    New Member
    • Feb 2008
    • 217

    #1

    How to Destroy Session On Window Close ???

    Hi,
    I am maintaning the status of users in database who are online... and also it makes false the status whenever user gets logout but my problem is that if user closes the window without logout at that time i want to change the status as a false... so how can i do this ??? i have tried many things but still i am not getting the desired things... It's only changes after Session timeout period... so please suggest me some solution... it's urgent need....

    thanks...

    -malav.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You can do a number of things:
    You can attempt to fire some sort of javascript even on UnLoad()
    You can shorten the Session timeout (so it's much quicker)
    You can make the Session timeout VERY short and implement a heartbeat on every page to keep the session active.

    Comment

    • shweta123
      Recognized Expert Contributor
      • Nov 2006
      • 692

      #3
      Hi,

      Please refer this Article regarding your question.

      Comment

      • malav123
        New Member
        • Feb 2008
        • 217

        #4
        Originally posted by shweta123
        Hi,

        Please refer this Article regarding your question.


        Hi shweta,
        I have already used that article's concept but it only gives the alert i.e. "Killing the session", it does not affect the session abandon method.... and that's why it also doesn't affect the code that i have write in global.asax for change the status of active users... so where is the problem ??? pls reply...

        Comment

        • shweta123
          Recognized Expert Contributor
          • Nov 2006
          • 692

          #5
          Hi,

          Please see if this Article helps you

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            You will need to write that event. On the closing events in javascript you would fire off an xmlhttprequest call to your server to a special page that just abandon's the session (like say a logout.aspx page)

            Comment

            • malav123
              New Member
              • Feb 2008
              • 217

              #7
              Originally posted by Plater
              You will need to write that event. On the closing events in javascript you would fire off an xmlhttprequest call to your server to a special page that just abandon's the session (like say a logout.aspx page)


              Sorry but i am not getting properly what you are suggesting.... so please explain me again what to do ???

              Comment

              • malav123
                New Member
                • Feb 2008
                • 217

                #8
                Originally posted by shweta123
                Hi,

                Please see if this Article helps you

                Thanks shweta for response but i have reffered this link also... but still i have not got solution in that link...

                Comment

                • Frinavale
                  Recognized Expert Expert
                  • Oct 2006
                  • 9749

                  #9
                  Originally posted by malav123
                  Thanks shweta for response but i have reffered this link also... but still i have not got solution in that link...
                  Plater had the right idea in suggesting the OnUnload JavaScript Event. The problem is when that happens the page is already unloaded and you can't make a call to the server.

                  Instead you should use the OnBeforeUnload JavaScript event to make a call back to the server and log out the user.

                  [code=javascript]
                  window.onbefore unload = function()
                  {
                  //here you make your call to the aspx page to log out the user
                  //make sure you don't return anything or else the user will be prompted to make a decision as to whether or not they want to actually leave the page....
                  }
                  [/code]
                  This will log out the user as soon as the close the window or leave the webpage.
                  Just be aware that all of your full page post backs to the server will cause the page to be unloaded. This means that you should probably add another JavaScript function to "register" the controls for valid postbacks.

                  Eg

                  [code=javascript]
                  var _validPostback = false;
                  function RegisterValidPo stBack()
                  { _validPostback= true;}
                  window.onbefore unload = function()
                  { if(_validPostba ck==true)
                  {
                  //here you make your call to the aspx page to log out the user
                  //make sure you don't return anything or else the user will be prompted to make a decision as to whether or not they want to actually leave the page....
                  }
                  }
                  [/code]

                  In your Server Side code you will have to add the onclick JavaScript event to each control that posts back..

                  eg
                  [code=vbnet]
                  myButton.Attrib utes.Add("oncli ck","RegisterVa lidPostBack();" )
                  [/code]


                  -Frinny

                  Comment

                  Working...