Page 1 of 1
How to save a session after browser closed?
Posted: Tue Jul 19, 2011 3:26 am
by MicroBoy
How to save a session after browser closed? Because when I close the browser and open again the website the session_id changes. I want not to be changed until the user clears cookies and history browser, or until a specified time?
Re: How to save a session after browser closed?
Posted: Tue Jul 19, 2011 7:48 am
by phazorRise
Sessions are meant to be expired on closing browser. It is in general. However, you can keep sessions alive by editing php.ini file.
see .
http://php.net/manual/en/session.configuration.php for more info.
Re: How to save a session after browser closed?
Posted: Tue Jul 19, 2011 11:25 am
by beetree
You can do this through a persistent cookie. If you have a lot of data in your session you probably want to consider server-side sessions or even saving only an ID in the cookie and handling the data structures yourself.
Re: How to save a session after browser closed?
Posted: Tue Jul 19, 2011 11:33 pm
by Pazuzu156
This is pretty simple, when a user logs in if you are using a login table, you create the session based on the username and/or password, hashed and salted for security. Then set a cookie that will be equal to the session. When the session is destroyed (browser closed) and then they navigate back to that page, check if the current session id is still open, if not then the cookie will be, so keep them logged in through the cookie, but then take the user's username from the mysql table and hash it, and randomize the string as the session's new id, and set the cookie equal to the new session id. This only cycles as the user is logged in when the browser is closed, once they log out then the cookie is destroyed and the cycle is broken. If you need me to come up with an example code ask and I will. It may not be the professional way, but I use this method.
Re: How to save a session after browser closed?
Posted: Wed Jul 20, 2011 8:11 am
by MicroBoy
Pazuzu156 wrote:This is pretty simple, when a user logs in if you are using a login table, you create the session based on the username and/or password, hashed and salted for security. Then set a cookie that will be equal to the session. When the session is destroyed (browser closed) and then they navigate back to that page, check if the current session id is still open, if not then the cookie will be, so keep them logged in through the cookie, but then take the user's username from the mysql table and hash it, and randomize the string as the session's new id, and set the cookie equal to the new session id. This only cycles as the user is logged in when the browser is closed, once they log out then the cookie is destroyed and the cycle is broken. If you need me to come up with an example code ask and I will. It may not be the professional way, but I use this method.
Thanks I did in this way, if the cookie exist than use the cookie if it doesn't, start the session and set the cookie.