Wednesday, April 17, 2013

I've found session_set_cookie_params to be ineffective at changing the session lifetime

I've found session_set_cookie_params to be ineffective at changing the session lifetime, to change the session lifetime I do the following:

<?php

$Lifetime
= 3600;
$Seperator = (strstr(strtoupper(substr(PHP_OS, 0, 3)), "WIN")) ? "\\" : "/";
$DirectoryPath = dirname(__FILE__) . "{$Seperator}SessionData";
is_dir($DirectoryPath) or mkdir($DirectoryPath, 0777);

if (
ini_get("session.use_trans_sid") == true) {
   
ini_set("url_rewriter.tags", "");
   
ini_set("session.use_trans_sid", false);

}

ini_set("session.gc_maxlifetime", $Lifetime);
ini_set("session.gc_divisor", "1");
ini_set("session.gc_probability", "1");
ini_set("session.cookie_lifetime", "0");
ini_set("session.save_path", $DirectoryPath);
session_start();

?>

I switched to this after having many failed attempts at having a session remain active for longer than the default 24 minutes, the problem it seems is what many suggest that appears to be wrong - Setting the cookie lifetime to be the same as session.gc_maxlifetime, although the expiry of the cookie is set, it isn't updated when the user remains active in the session, so even if the session is actively being used, once the cookie life runs out, the sessions gets invalidated, using the above seems to work for me.

No comments:

Post a Comment