What happens to session when browser is closed

February 26, 2015
  1. By default closing browser window doesn’t affect session.Session is still present.And when you reopen browser and visit your website it is restored.
  2. By default session’s lifetime is set at server side(“session.gc_maxlifetime” variable in “php.ini” file)
  3. By default your session will expire after your inactive time exceeds session’s lifetime.
  4. Garbage collector runs periodically to find out expired sessions and destroys them.
  5. After this your session will no longer available.
  6. Looking at client/browser side
  7. Session Id is stored in cookie in your browser.
  8. When you reopen browser,session id is sent to server.Server look if session is available for that id(if it is not destroyed yet) and restores the session.
  9. But this default behaviour can be changed.You can do this at two levels.Server side and client side.
  10. At server site you can modify “session.gc_maxlifetime” variable in “php.ini” file to change the session life time.
  11. At client side you can modify session lifetime by “session_set_cookie_params($lifetime_value)” function which must be called before you start session.
  12. Setting session lifetime at client side just tells browser that how much time session id will be stored in browser.
  13. So if you call “session_set_cookie_params($lifetime_value)” with non zero value,your session (session id stored in cookie) will expire after lifetime value irrespective of active/inactive time.
  14. So if you call “session_set_cookie_params($lifetime_value)” with lifetime value “0”,your session will expire on closing browser window.
  15. By doing so when you reopen browser and visit the web page there will not be any session id to send to the server.And thus your session will not be restored.But remember it is still present at server side until garbage collector runs.

No Comments

Leave a Reply

Your email address will not be published. Required fields are marked *