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.

2 comments:

Kiran said...

When i use ini_set('') it's showing correct value for session expiration time.

But when i tried to use htaccess code its getting 500 error. Whats the reason?

Beski said...

I dont know the exact reason... Its working fine for me using htaccess.

But,
Increasing session timeout is not reliable, now i use cookies for long life user session. Increasing cookie lifetime is so easy and reliable.

http://w3schools.com/php/php_cookies.asp