Click to See Complete Forum and Search --> : PHP Sessions.


SuzanneB
01-09-2007, 03:55 AM
I really don't have much understanding of sessions. My use of them is very basic. I typically use them to remember information concerning a customer. The problem is that the sessions only persist until the browser is shut down. Is it possible to extend sessions so that they remain ( assuming that the person has cookies enabled ) in place even after the bowser has been shut down? I see this is done on some nice web sites, and I am wondering whether this is a combination of sessions and cookies or whether this is just a process using sessions that I don't understand.

NogDog
01-09-2007, 05:24 AM
If there is just a small amount of data that you want to persist, and if it does not contain any data that you would consider sensitive, then I'd probably just use cookies to save it (specifying a cookie expiration time as far in the future as desired.) If any of the data is sensitive, or if there is a lot of it, or if you want to make sure it persists even if the user clears their cookies or moves to a different computer; then I'd probably look into storing it in a database. And of course you could use a mixture of both (along with sessions, too).

SuzanneB
01-09-2007, 07:18 AM
Hello NogDog! Still here then? Happy New Year!

It is to remember the customer should they accidentally close their browser window. When they are buying items, everything is placed in a php created file on the server, which is accessed via a session id. The problem is that if they accidentally close their browser window, then the session id is lost. What I need to do is to remember the session id even when they close the window. This I can do, of course, by storing it as a cookie on their computer ( if they have cookies disabled, then, ok, fair enough, it forgets!! I mean we can't cover every eventuality!) But it seemed to me that this is almost what the PHP session itself is doing, so I wondered whether I needed new code or whether it was possible to just modify some session parameters that I was unaware of.

NogDog
01-09-2007, 02:03 PM
To extend the life of the session values, you would want to change three php.ini settings:
session.cookie_lifetime
session.gc_maxlifetime
session.save_path
(You want to set the last one so that any other PHP scripts on the same server using sessions to not "overrule" your session.gc_maxlifetime setting. You'll also need to make sure that your PHP scripts have read/write permission on that directory.)

You could accomplish this by starting each applicable script with:

<?php
session_save_path($_SERVER['DOCUMENT_ROOT'].'/session_data');
session_set_cookie_params(3600); // 3600 seconds = 1 hour
ini_set('session.gc_maxlifetime', 3600);
session_start(); // this needs to be done after the above lines

Alternatively, if running on Apache you could make those settings in a .htaccess file.