How to save a session after browser closed?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MicroBoy
Forum Contributor
Posts: 112
Joined: Sat Mar 14, 2009 5:16 pm

How to save a session after browser closed?

Post 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?
User avatar
phazorRise
Forum Contributor
Posts: 134
Joined: Mon Dec 27, 2010 7:58 am

Re: How to save a session after browser closed?

Post 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.
beetree
Forum Commoner
Posts: 26
Joined: Mon Jul 18, 2011 6:30 pm
Location: Peninsula

Re: How to save a session after browser closed?

Post 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.
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: How to save a session after browser closed?

Post 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.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
MicroBoy
Forum Contributor
Posts: 112
Joined: Sat Mar 14, 2009 5:16 pm

Re: How to save a session after browser closed?

Post 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.
Post Reply