How to extend session life in Symfony2?

I have a checkbox for logging in with remembering. I try to set the session duration to 1 year, if the checkbox is remembered, but regardless of what I tried, it does not work ... Symfony profiler always says that the session lifetime is 0.

What I tried in my controller function:

$this->getRequest()->getSession()->set("gesaudit",array("login"=>true,"user"=>$user)); if($remember == "1"){ $lifetime = new NativeSessionStorage(); $lifetime->setOptions(array('cookie_lifetime' => 31557600)); }else{ $lifetime = new NativeSessionStorage(); $lifetime->setOptions(array('cookie_lifetime' => 0)); } 
+5
source share
2 answers

According to the documentation, you can control the normal session lifetime through config.yml:

 framework: session: cookie_lifetime: 3600 

To set the service life per year: 3600 * 24 * 365 = 86400 * 365 = 31536000. Easy :).

This sets the session cookie lifetime to 1 hour. There are many other things you can customize, but why did I include a link to the documentation.
If you insist on doing it manually, in a specific case / controller, pass an array of parameters to the controller. Perhaps this will work instead of using the setOptions method: if your php.ini contains:

 session.auto_start = 1 

Then instantiating the NativeSessionStorage will automatically and immediately create a session with a default lifetime. After that, the setting will be insignificant or no difference. Check ini settings or follow these steps:

 $test = new NativeSessionStorage(); var_dump($test->isStarted()) 

If it unloads true , try:

 $lifetime = new NativeSessionStorage( array( 'cookie_lifetime' => 31536000 ) ); 

Symfony2 sessions can be found here .

If you are using Symfony2.4, there is a special section in the documents on catchy features , as Jakub Polak noted. Its essence is that the flag should be called by _remember_me , that config.yml should determine the value of %secret% , and that you add a (customized) version of this file to the security.yml file:

 firewalls: main: remember_me: key: "%secret%" lifetime: 31536000 path: / domain: ~ # Defaults to the current domain from $_SERVER 

But the documentation explains all of this, but you have to turn the manual around a bit. For example, if you want to specify different mem-me behavior for certain sections, change main in the yml snippet above and add the pattern parameter as here .
It is probably best to scan the entire security section .

+15
source

This should be done in the security.yml configuration file:

 secured_area: pattern: ^/yourpattern remember_me: key: "%secret%" lifetime: 31536000 # 365 days in seconds path: /yourpath domain: .yourdomain.com 

You can find the relevant documentation here .

+1
source

All Articles