What happens to session when browser is closed
- 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.
- By default session’s lifetime is set at server side(“session.gc_maxlifetime” variable in “php.ini” file)
- By default your session will expire after your inactive time exceeds session’s lifetime.
- Garbage collector runs periodically to find out expired sessions and destroys them.
- After this your session will no longer available.
- Looking at client/browser side
- Session Id is stored in cookie in your browser.
- 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.
- But this default behaviour can be changed.You can do this at two levels.Server side and client side.
- At server site you can modify “session.gc_maxlifetime” variable in “php.ini” file to change the session life time.
- At client side you can modify session lifetime by “session_set_cookie_params($lifetime_value)” function which must be called before you start session.
- Setting session lifetime at client side just tells browser that how much time session id will be stored in browser.
- 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.
- So if you call “session_set_cookie_params($lifetime_value)” with lifetime value “0”,your session will expire on closing browser window.
- 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.
About The Author