Saturday, December 12, 2009

How to increase the PHP session timeout time in a shared host?

I found this link useful.

(In a shared host, you've got to have your own folder to store your session data; otherwise, the garbage collector (which is invoked with the probability session.gc_probability(default value is 1)/session.gc_divisor (default value is 100) based on the global session timeout time session.gc_maxlifetime) may inadvertantly erase your session data).

Here's what I have at the top of each php file (extracted from the above link):


<?php
$id
= session_id();
if (
is_null($id) || strcmp($id, "") == 0) {
$cookie_path = "/";
$cookie_timeout = 60 * 60; // in seconds
$garbage_timeout = $cookie_timeout + 600; // in seconds
session_set_cookie_params($cookie_timeout, $cookie_path);
ini_set('session.gc_maxlifetime', $garbage_timeout);

strstr(strtoupper(substr($_SERVER["OS"], 0, 3)), "WIN") ?
$sep = "\\" : $sep = "/";
$sessdir = ini_get('session.save_path').$sep."ek_sessions";
if (!
is_dir($sessdir)) {
mkdir($sessdir, 0777);
}

ini_set('session.save_path', $sessdir);

session_start();
}
?>