Welcome Guest, Not a member yet? Register   Sign In
Destroy session on window close
#1

[eluser]bikuta[/eluser]
Hi guys,

I'm using the CI Session library and I need a way to destroy the session when the user closes the browser. Is there anyway to do this? Or is there a better way of doing it?
#2

[eluser]bikuta[/eluser]
So I guess the only way to do this is to use a Javascript hack?
#3

[eluser]Aea[/eluser]
Yes, and two ways to go about it (if you want the direct functionality).

- Very short session expire timers and JS updates sent to keep it active. This is a bit of a hack but it's reliable.
- Sending an AJAX request on close of the window.

or

- Semi short times so that session expire within a few minutes of the last action.
#4

[eluser]Rick Jolly[/eluser]
PHP Native sessions (and the Native Session library in the Wiki) expire when the browser closes by default. That's because the session cookie is stored in memory on the client computer. Keep in mind that Firefox shares memory across all open browser windows and tabs and IE shares memory across tabs.
#5

[eluser]bikuta[/eluser]
is there anyway for me to have that behaviour with CI sessions?
#6

[eluser]Aea[/eluser]
No, because session cookies aren't stored in memory for most browsers, they're stored on disk.
#7

[eluser]bikuta[/eluser]
Was that a no, as in I can't have the same behaviour as the PHP native session (as stated in post #4)?
#8

[eluser]Aea[/eluser]
PHP will not have that behavior, sessions do not magically expire when the browser closes because they are stored on the disk, which is why you can access a site using the same cookie even after you restart your browser Wink Of course if you set a low cookie life span you can get a similar effect, which is what most secure applications do (and then send you a javascript warning that your session is expiring and can be extended by clicking okay).
#9

[eluser]bikuta[/eluser]
Ok, so if I use a short cookie life span. I want to be able to automatically extend the session period using JS, I would probably make a request to the server using AJAX. However does it matter what kind of request I'm doing? i.e., could I just be calling a blank page?
#10

[eluser]Aea[/eluser]
I don't have time to look into the CI Session, but yes, just make an AJAX request to a controller which returns nothing named refresh_session (or something to that extent) and set your cookie life span to 5 minutes and do that call every two and a half (or so).




Theme © iAndrew 2016 - Forum software by © MyBB