Home    Articles

 

Set Zend Session timeout/expiration time

 

Accordingly to this source ( Zend Session Documentation: here ) you can set two ways for your session to expire:

First of all you need to use Zend Session php class to start your session. You cannot use session_start() ;

Then you can create as many keys as you need on that session object you've created.

Therefore you can set expiration time on entire session or on keys.

First way to set timeout for your zend session is:

1. Using seconds

1.a Both: username and userid will expire after 120 seconds

$session =  new Zend_Session_Namespace('Session');
$session->username = 'John';
$session->userid = 1;

$session->setExpirationSeconds(120); // will expire after 120 seconds or 2 minutes

1.b Each session key will expire differently

$session =  new Zend_Session_Namespace('Session');
$session->username = 'John';
$session->userid = 1;

$session->setExpirationSeconds(120,'username'); // will expire after 120 seconds
$session->setExpirationSeconds(30,'userid'); // will expire after 30 seconds

//.

2. Using hops ( browser requests )

In the same way as "using secconds" you can set entire session to expire or different keys.

You will find out that your session will expire after a specific requests number set by you.

I'm not sure what are the session timeout limits but this should be very helpful.

$session =  new Zend_Session_Namespace('Session');
$session->username = 'John';
$session->userid = 1;

$session->setExpirationHops(10); // expire entire session after 10 requests / "hops"
// OR
$session->setExpirationHops(10,'username'); // expire username after 10 requests / "hops"
$session->setExpirationHops(40,'userid'); // expire userid after 40 requests / "hops"

That's all.

Enjoy.

by