Editing SESSION lifetime

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Editing SESSION lifetime

Post by aceconcepts »

Hi All,

Can anyone supply a definitive solution as to how one can edit (increase/decrease) SESSION lifetime?

Thanks.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Editing SESSION lifetime

Post 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']
    );
}
 
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Editing SESSION lifetime

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Editing SESSION lifetime

Post 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.
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

Re: Editing SESSION lifetime

Post 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
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Editing SESSION lifetime

Post 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.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Editing SESSION lifetime

Post by aceconcepts »

I've tried both pieces of coded posted however, it still doesn't work.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Editing SESSION lifetime

Post 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.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Editing SESSION lifetime

Post by aceconcepts »

Yeh, sorry about blunt reply - i'm almost falling asleep at work :wink:

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)
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Editing SESSION lifetime

Post by Eran »

And how are you testing it to see if it's working? where is it failing?
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Editing SESSION lifetime

Post by aceconcepts »

Well from the above you can see that i get the session lifetime var as soon as i edit it.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Editing SESSION lifetime

Post 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.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Editing SESSION lifetime

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Editing SESSION lifetime

Post 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)
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Editing SESSION lifetime

Post 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.
Post Reply