Home » Asp.netRSS

End Session When Browser Closed

Hi, I am quite new to asp.net and web tech.

I created a site something like bookstore, and now I am creating shopping cart for it.

I used datatable which i store in session for store shopping cart data.

The problem is that, I put some items in cart and then close the browser immediately and next time when i open it the data are stills there unless the session is expire.
I like to abandon the session when user close the browser.
Is there any solution for it and is this a good way to create my shopping cart?

Thx in advance.

MgKo
 

 

17 Answers Found

 

Answer 1

Why don't you empty all sessions on Page Load? 

 

Answer 2

I don't think emptying all sessions on page Load will work. Cos then whenever the user posts back to the page, the session will be emptied.

Try emptying all the session in Session_Start of the global.asax. That might work. Not 100% sure though.

 

Answer 3

Why don't you empty all sessions on Page Load? 

Amin
http://360.yahoo.com/amins2s

 <<<<<<<<<<<<<<<<<<<<

I tried that but its clear everytime page is refresh, It doesn't work.

 

Answer 4

I have a question. Where are you storing the contents of the shopping cart ? In a session object or the Application Data Cache ?

 

Answer 5

Hi,

 Sorry, you already stated that you are saving it in a session object.

 It is very weird that your shopping cart info is still displayed after the browser is closed and reopened. It should be reset on closing the browser

 There is one thing you could try, which is storing the SessionID in the url. In the web.config file put the following segment.

<system.web>

<sessionStatecookieless="true"></sessionState>

</system.web>

 I hope this helps.

 

 

Answer 6

HI, mgkoko: 

There is no full-proof technique, but here is some probable solutions ...

When a user closes the browser, the session doesn't literally expire for a default time of 20 minutes. When the session expires the OnEnd event fires. You can code against this in the global.asax file like so ...

    Sub Session_OnEnd()
        Dim conn As New SqlConnection(CONNECT_STRING)
        Dim cmd As New SqlCommand( _
            "insert into _DummyTbl (Text) values ('GLOBAL')", conn)

        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
    End Sub

Now if you call the Session.Abandon method the OnEnd event will fire immediately. (NOT quite though, because I noticed that it took up to a few minutes at certain times for a record to appear into the database, but this is the best we are going to get.) Now we need some way to call the Session.Abandon method when a user closes their browser. This trick wont work in all web browsers, but you can tap into the onunload() event like so ... TESTA.ASPX

<body onunload="window.location.href='TestB.aspx';">
...

When the user closes the browser window at TestA.aspx, they are redirected to TestB.aspx ... TESTB.ASPX

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
   Call MyBase.OnLoad(e)

   ' End The Session
   Session.Abandon()

   ' Build A JavaScript String That Will Close This Web Browser Window
   Dim s As String = ""
   s &= "<script language=""javascript"">"
   s &= "window.close();"
   s &= "</script>"
   
   ' Add The JavaScript To The HTML Stream
   Page.RegisterClientScriptBlock("close", s)
End Sub



On page load event of TestB.aspx, the code abandons the session then closes the browser immediately. The user never sees this page. In fact to the user, it looks like they closed TestA.aspx.

This javascript technique speeds up the process of killing the session and calling Session_OnEnd, but is not supported on all browsers. For older browsers, the application is just gonna have to suffer the 20 minute session timeout period.

I hope the above information will be helpful. If you have any issues or concerns, please let me know. It's my pleasure to be of assistance

 

Answer 7

Thank you all, my mistake, I've got it.
 

Answer 8

what was the mistake and what was the solution?

 

Answer 9

 

Sorry, Tarique_s I should have tell you,

Actually it cleans all the session when you close the whole browser.

The problem seems that When I open multiple tabs in one browser and my website is running on one of those tab.

I close my website tab and reopen it in new tab, the session is still there.

but If I close the whole explorer, and reopen it in new browser it clear the session.

I didn't write any code in Global.asax. 

it's funny, I don't know why is that. maybe I don't understand well enough in session and application.

sorry my eng is a bit poor, and you might not quite understand,

if you have further question please let me know

cheers,

best regard

MgKo 

 

 

Answer 10

There is no way to detect the Session_End on browser close .  You can do it by adding some extra logic in Page_Load like  If (!IsPostBack){ }. but make sure this page is not being called from any other page within the site. If this is the case you can use the query string to detect from where the page is coming from.

 

Answer 11

Yes there is

Session_End event get fired when you close the browser. at least it does for me :)

 

Answer 12

Session_end does *not* get fired when you close the browser. Session_End gets fired when the Session Timeout has expired. It happens on the server, and is completely independent of whether the user's browser is open or closed. Unless you use one of the mostly unreliable hacks such as a call created by onUnload browser dom event, the server has no idea whether the browser is open or closed.

 

Answer 13

I found this article and it was very helpfull

CodeSnip: Handle Browser Close Event on the Server-Side

 

Answer 14

i hav a similar prob.. i have a flag in my login table which is set to 'Y' when logged in and 'N' when logget out... it works ..with logout link/button ..

but if user closes the browser window i cant update the flag to 'N'...

I HAV CODE IN Global.asax on 'Session_End' like.. 

"Session End") and then

MsgBox(

the update statement to update the flag to 'N' ...now when the browser is closed i can only see the Messagebox but the flag is unchanged... that to after a long time the messagebox appears..!!  where and how should i control/edit the session timeout...?

PLZ HELP ME OUT WITH A SOLUTION .. am not familiar with ajax or any other scripting.. plz provide a simple [sample] code.. :)

thanks a lot..in advance..

cheers.. 

---gforgourav

 

Answer 15

Hi ,

when user close the browser find the unload event and

put Session("SessionName") = Nothing

 

Thanks

Ramesh

 

Answer 16

This code will clears the session when browser tab is attempted to close.

<body onclick="clicked=true;" onunload="CheckBrowser()"> <script type ="text/javascript" > var clicked = false; var xmlHttp var browser = navigator.appName; function CheckBrowser() { if (clicked == false) { xmlHttp = GetXmlHttpObject(); xmlHttp.open("GET", "SessionEnd.aspx", true); xmlHttp.onreadystatechange = function() { if (xmlHttp.readyState == 4) { // alert(xmlhttp.responseText) } } xmlHttp.send(null) if (browser == "Netscape") { window.location=".../SessionEnd.aspx"; alert("Browser Terminated"); openInNewWindow(); } } else { //alert("Redirected"); clicked = false; } } function GetXmlHttpObject() { var xmlHttp = null; try { // Firefox, Opera 8.0+, Safari xmlHttp = new XMLHttpRequest(); } catch (e) { //Internet Explorer try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttp; } function openInNewWindow() { // Change "_blank" to something like "newWindow" to load all links in the same new window var newWindow = window.open(".../SessionEnd.aspx"); newWindow.focus(); return false; } </script>

I've added SessionEnd.aspx an extra form just to do session clear process and alert again by popping new tab.

You can remove that part if you dont want redirection.


 

Answer 17

Hi

I am using master page... so there would be only one form tag. i want to handle browser close event and then kill the session. how can i do this?expecting for all browsers , not just IE.

any help would be highly appreciated.

 

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter