Symfony2 session lifetime

I am having a problem with the symfony2 session component. I set some data for the session through the session container as follows:

$sess = $this->get( 'session' ); $sess->set( 'some_key', 'some_value' ); 

But after a while (about 15-20 minutes), the session was lost.

Can I set the session lifetime parameter? An ideal option for me would be if I could set a specific session time in real time ... Can someone help?

+24
source share
3 answers

You can set the session expiration time in your configuration file in the framework section. My looks like this:

config.yml

 framework: secret: %secret% charset: UTF-8 error_handler: null csrf_protection: enabled: true router: { resource: "%kernel.root_dir%/config/routing.yml" } validation: { enabled: true, annotations: true } templating: { engines: ['twig'] } #assets_version: SomeVersionScheme session: default_locale: %locale% cookie_lifetime: 3600 // was "lifetime" but deprecated auto_start: true 

You can change the value of framework.session.lifetime to whatever you want in seconds (by default it is 3600 or 1 hour).

Link here .

+45
source

In Symfony 2.3, I think the correct answer is found in app / config / config.yml:

 framework: session: cookie_lifetime: 7200 gc_maxlifetime: 3600 

GC (garbage collection) will be reset every time the server hits, that is, if the user is active, he will have 3600 to continue working. cookie_lifetime will force the user to fail. In this example, the user will have one hour to be inactive and will be forced out after 2 hours.

+28
source

To work conveniently, you can set the cookie_lifetime to 0 in the dev environment, which means that the cookie expires when the browser is closed.

File: config_dev.php

  framework: session: cookie_lifetime: 0 
+4
source

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


All Articles