Click to See Complete Forum and Search --> : Setting a Session to Time Out?


Joseph Witchard
06-19-2008, 02:51 PM
How do you do this?

SyCo
06-19-2008, 04:44 PM
ini_set(’session.gc_maxlifetime’, 30*60);


Or I found this on another forum

if(!session_is_registered("session_count")) {
$session_count = 0;
$session_start = time();
$_SESSION['session_count']=$session_count;
$_SESSION['session_start']=$session_start;
} else {
$session_count++;
}

$session_timeout = 3600; // 1hr (in sec)

$session_duration = time() - $session_start;
if ($session_duration > $session_timeout) {
header("Location: logout.php"); // Redirect to Logout Page
}
$session_start = time();

Joseph Witchard
06-19-2008, 05:09 PM
I like the second bit of code a lot better (I understand it better than the first), but I'd rather not use the session_is_registered function. Is there a more recent way to accomplish the same effect without that function?

SyCo
06-19-2008, 05:15 PM
First is just a script based way to alter the ini settings on shared hosting. I you have access to the ini file you cna edit that value directly.

In the second you can replace
if(!session_is_registered("session_count")) {

with
if(!isset($_SESSION['session_count'])) {

NogDog
06-19-2008, 05:16 PM
Either set the session.cookie_lifetime (http://www.php.net/manual/en/session.configuration.php#ini.session.cookie-lifetime) value to the desired session duration in php.ini or a local .htaccess file, or set it before the call to session_start() in each script via the session_set_cookie_params (http://us2.php.net/manual/en/function.session-set-cookie-params.php)() function.

You can then set the session.gc_maxlifetime value to have session data cleaned up on the host after the desired amount of time, but also read about all the related "gc" settings on this page (http://www.php.net/manual/en/session.configuration.php) to see how they interact. These can all also be set globally via php.ini or locally via .htaccess, or per script via ini_set (http://www.php.net/ini_set). But note that if other scripts share the same session data directory, their "gc" settings may conflict with those of your scripts where you set it locally or via ini_set(). Again, read the above link carefully for how all these parameters interact.

Joseph Witchard
06-19-2008, 05:23 PM
Thanks. How would you set a session to time out when a button was clicked? Like a logout button?

NogDog
06-19-2008, 05:26 PM
See the example on the session_destroy() page (http://www.php.net/manual/en/function.session-destroy.php).

Joseph Witchard
06-19-2008, 05:31 PM
Thanks!