Stephen Watkins
Stephen Watkins

Reputation: 25765

Programmatically Set Session Lifetime in Symfony2

I have been researching online, but could not find the answer. In Symfony2, I understand that one can statically set the session lifetime through the config.yml file.

However, I need to be able to set the lifetime of the session based on certain situations in my code. Is it possible to programmatically set the session lifetime?

Upvotes: 2

Views: 2376

Answers (2)

scc
scc

Reputation: 10696

Depending on the case (not on post requests with csrf protection in form for example), migrate can be used as well:

$request->getSession()->migrate();

According to the docs: "Migrates the current session to a new session id while maintaining all session attributes."

Upvotes: 0

Femi
Femi

Reputation: 64700

It appears that in Symfony2 you can't change the attributes of the session storage container on the fly: see https://github.com/symfony/HttpFoundation/blob/master/SessionStorage/SessionStorageInterface.php for the actual implementation interface that is made available, and notice there is nothing in there that lets you change the lifetime value.

However, the session classes by default use the session_get_cookie_params method to set the session lifetime: you can adjust those values by calling session_set_cookie_params (preferably before the session is initialized: try to call it as early as possible in the controller). See if that works for you.

Upvotes: 6

Related Questions