Wednesday, August 13, 2008

Change / Increase session timeout in php

Projects using login may have the problem of expiring sessions in short time.
How to increase the session timeout in PHP?

use this as first line - before session_start
ini_set('session.cookie_lifetime',(3600*2));

In some servers it may not work.... BUT using .htaccess file it is working very fine.
Just place these lines in the .htaccess file and put the file in your root directory.
php_value session.cookie_lifetime 3600
php_value session.gc_maxlifetime 3600

and note these parameters are in seconds.
Then how to check the current session timeout?
use this:
printf("cookie: %s, gc: %s", ini_get('session.cookie_lifetime'), ini_get('session.gc_maxlifetime'));

Thats all.