Programmatically set session lifetime in Symfony2

I study online, but could not find the answer. In Symfony2, I understand that you can statically set the session lifetime through the config.yml file.

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

+2
source share
2 answers

It looks like in Symfony2 you cannot 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 one that is available, and notice that in it there is nothing that allows you to change the value of the lifetime.

However, session classes by default use the session_get_cookie_params method to set the session lifetime: you can set these values ​​by calling session_set_cookie_params (preferably before session initialization: try to name it as early as possible in the controller). See if this works for you.

+6
source

Depending on the case (not for mail requests with csrf protection in the form, for example), you can also use migration:

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

According to the docs: "Transferring the current session to a new session identifier while retaining all session attributes."

0
source

Source: https://habr.com/ru/post/904362/


All Articles