3

I'm using Symfony2 and I would like to logout the user after he closes the browser.

I've changed the cookie_lifetime to 0 inside of the config.yml, but still not working.

The idea is:

  • if the user doesn't use the REMEMBER ME, I would like to ask him to login in case of he close the browser and open again.
Canela
  • 160
  • 1
  • 1
  • 11

2 Answers2

0

I found that there's no really reliable way check wether the browser-window/tab has been closed even if you use a cookie lifetime of 0 due to the way some browsers handle the cookies.

However another - obviously not always reliable - way to check wether the window/tab was closed is using JavaScript's unload events.

The combination of both should give you the best results.

Examples - vanilla JavaScript:

window.onbeforeunload = function(){
  // send ajax request to invalidate the session
};

... or ...

window.unload = function(){
  // send ajax request to invalidate the session
};

Example - jQuery:

$(window).bind('beforeunload', function(){
  // send ajax request to invalidate the session
});

... or ...

$(window).unload(function(){
  // send ajax request to invalidate the session
});

More information can be found in this question.

Community
  • 1
  • 1
Nicolai Fröhlich
  • 51,330
  • 11
  • 126
  • 130
  • 1
    uhm, you don't have to do the ajax call twice. Use either `beforeunload` (preferred) or `unload` function. Although the cookie should be checked too (if the OP is using cookies). – KarelG Feb 28 '14 at 06:48
  • Jeah i see that's misunderstandable ... i just wanted to include all the available options. updated the answer. Thanks for pointing out. – Nicolai Fröhlich Feb 28 '14 at 06:49
  • Thank you! I checked and the problem was the browser :( See the answer below. – Canela Feb 28 '14 at 22:51
0

I assume you are using Chrome browser. Chrome has a problem with clearing such cookies (see the discussion )

Other browsers (FF, Safari) works fine and deletes cookies when browser closes, try it youself.

Community
  • 1
  • 1
Dmitry Skryabin
  • 1,584
  • 2
  • 10
  • 15
  • Perfect! @Dmitry Skryabin. – Canela Feb 28 '14 at 21:25
  • Skryabin. I checked and just Chrome had this behaviour. It happened because I was using the option "Continue running background apps when Google Chrome is closed" to keep checking my emails while the browser is off. After I've disabled this option the chrome stopped keeping my temporary cookies. – Canela Feb 28 '14 at 21:31