How to end session after closing the window?

Posted on 2005-04-22
Last Modified: 2012-05-05

How can I end ASP session (programmatically) after I close browser?

Please help.
Question by:JanWale
    LVL 5

    Assisted Solution


    I don't think you can kill session when the broswer closes, since sessions expire automatically after a period of time that is set in your server.

    One alternative is try to use cookies combination since they stay in client side.

    Other Alternative is  you can write <body unload="'logout.asp')"> but I don't think it's very reliable/cross browser compatible.

    in logout.asp:

    //kill session


    LVL 6

    Expert Comment

    I think you can do something like this

    <SCRIPT language="JavaScript">
    function loadOut()

    <body onBeforeUnload="loadOut()">
    LVL 6

    Expert Comment

    Session object is automatically destroyed after window closed.
    LVL 1

    Author Comment

    >Session object is automatically destroyed after window closed.

    So why when I close the window and reopen new one my application took me to user that I logged-in in the first time?!
    LVL 8

    Expert Comment

    by:Faheem Shaikh
    This might help you::The code below might give you some hints.

    Create a new file global.asa containing following code::

    <SCRIPT LANGUAGE='VBScript' RUNAT='Server'>
    'You can add special event handlers in this file that will get run automatically when  
    'special Active Server Pages events occur. To create these handlers, just create a
    'subroutine with a name from the list below that corresponds to the event you want to
    'use. For example, to create an event handler for Session_OnStart, you would put the
    'following code into this file (without the comments):
    'Sub Session_OnStart
    '**Put your code here **
    'End Sub
    'EventName              Description
    'Session_OnStart        Runs the first time a user runs any page in your application
    'Session_OnEnd          Runs when a user's session times out or quits your application
    'Application_OnStart    Runs once when the first page of your application is run for the first time by any user
    'Application_OnEnd      Runs once when the web server shuts down

    Sub Session_OnStart
    ' Create a FileSystemObject to provide files in the script
          Set Session("FSO") = CreateObject("Scripting.FileSystemObject")
          ' Create a variable that has the number of files created in this session
          Session("n") = 0
                Session("allowed") = false
    End Sub

    Sub Session_OnEnd
          Dim x
          For x = 0 to Session("n")-1
                Session("FSO").DeleteFile Session("sTempFile" & x), True
          Session("allowed") = false
    End Sub


    LVL 6

    Accepted Solution

    It is impossible, if you use only Session object, not Cookie, it takes you to logged in page.
    Maybe it's cache or something.
    Please double check. I can assure you that when browser closed, session object terminated.
    Unless of course, if you logged in another window and continue from there again.
    When new window opened, that browser will act as "logged-off" not depending of your "logged-in" browser closed or not.

    Please, provide process of you authentication method and check cookie. If cookie is set, it will not expire till expire time.

    Ask for more if unsure.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
    I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
    This video discusses moving either the default database or any database to a new volume.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now