Page 1 of 2
Editing SESSION lifetime
Posted: Wed Nov 26, 2008 4:47 am
by aceconcepts
Hi All,
Can anyone supply a definitive solution as to how one can edit (increase/decrease) SESSION lifetime?
Thanks.
Re: Editing SESSION lifetime
Posted: Wed Nov 26, 2008 5:01 am
by Eran
Using
session_set_cookie_params() -
Code: Select all
function sessionLifetime($seconds) {
$cookieParams = session_get_cookie_params();
session_set_cookie_params(
$seconds,
$cookieParams['path'],
$cookieParams['domain'],
$cookieParams['secure']
);
}
Re: Editing SESSION lifetime
Posted: Wed Nov 26, 2008 5:05 am
by aceconcepts
Hi pytrin,
Thanks for the snippet of code. This code will only work with cookies though right? I am primarily looking for a solution for $_SESSION max_life_time editing.
Re: Editing SESSION lifetime
Posted: Wed Nov 26, 2008 5:11 am
by Eran
No, this is for sessions. Unless you are using session.use_cookies=0, every session created stores its session id using a cookie. Otherwise it must be passed through the URL, which modern applications try to avoid for security reasons.
Re: Editing SESSION lifetime
Posted: Wed Nov 26, 2008 5:13 am
by pcoder
For this you have to overwrite the php.ini's value.
Code: Select all
ini_set(’session.gc_maxlifetime’, ‘28800?);// Set maxlifetime to 4 hours
Cheers
Re: Editing SESSION lifetime
Posted: Wed Nov 26, 2008 5:18 am
by aceconcepts
pcoder wrote:For this you have to overwrite the php.ini's value.
Code: Select all
ini_set(’session.gc_maxlifetime’, ‘28800?);// Set maxlifetime to 4 hours
Cheers
Yes, ini_set is what I have played about with but to no avail.
@pytrin: Thanks for the confirmation. I'm going to give it a whirl and see how it works.
@pcoder: Also thanks to you for your post. I will give this a try too.
Will post back soon.
Re: Editing SESSION lifetime
Posted: Wed Nov 26, 2008 10:03 am
by aceconcepts
I've tried both pieces of coded posted however, it still doesn't work.
Re: Editing SESSION lifetime
Posted: Wed Nov 26, 2008 10:18 am
by Eran
Well obviously for each of us, our suggested method works. Maybe you should explain what exactly doesn't work, post the code you used, some details on your environment, etc.
Re: Editing SESSION lifetime
Posted: Wed Nov 26, 2008 10:28 am
by aceconcepts
Yeh, sorry about blunt reply - i'm almost falling asleep at work
So this is how i'm testing it:
I tried the ini_set approach first.
Code: Select all
ini_set(’session.gc_maxlifetime’, 3600);
$lifetime=ini_get('session.gc_maxlifetime');
session_start();
if(!isset($_SESSION['logged']))
{
echo '<p>Not set</p>';
}
echo "<p>$lifetime</p>";
$_SESSION['logged']=1;
I even get the session maxlifetime config setting to determine if it's been changed.
I have PHP 4.4.4 running on Apache (Unix)
Re: Editing SESSION lifetime
Posted: Wed Nov 26, 2008 10:45 am
by Eran
And how are you testing it to see if it's working? where is it failing?
Re: Editing SESSION lifetime
Posted: Wed Nov 26, 2008 10:48 am
by aceconcepts
Well from the above you can see that i get the session lifetime var as soon as i edit it.
Re: Editing SESSION lifetime
Posted: Wed Nov 26, 2008 10:54 am
by Eran
You're going to have to be more specific than that..
What is not working for you? are sessions not registering at all? or do they not last their set lifetime? what is the value that $lifetime var? try var_dump it instead of echoing it also.
Re: Editing SESSION lifetime
Posted: Wed Nov 26, 2008 11:02 am
by aceconcepts
Ok, i'll be more specific.
Sessions are working fine in terms of storing data etc... sessions expire after the default lifetime of 24min (1440 seconds).
The $lifetime var simply outputs the current session lifetime set in php.ini - see line 4 from my post.
Re: Editing SESSION lifetime
Posted: Wed Nov 26, 2008 11:06 am
by Eran
The $lifetime var simply outputs the current session lifetime set in php.ini - see line 4 from my post.
.. and what is that value? is it not what you set before? did you try my suggested method? how did you test that? (since it doesn't change session.gc_maxlifetime)
Re: Editing SESSION lifetime
Posted: Wed Nov 26, 2008 11:08 am
by aceconcepts
$lifetime returned 1440.
I did try your suggested method but to no avail.
However, I have contacted my ISP and they tested it from their end and the ini_set method worked for them. They're currently looking into it.