0

On a Symfony 5.4 application running with PHP 8.1, the default configuration for sessions is as follows:

# config/packages/framework.yaml
framework:
    session:
        handler_id: null
        cookie_secure: auto
        cookie_samesite: lax
        storage_factory_id: session.storage.factory.native

It relies on the native PHP session management. Being on a Debian system, session files are stored in /var/lib/php/sessions/ and expired sessions are natively handled with a dedicated cleaner service (process known as garbage collection).

If you change the save_path option for another folder as stated in the doc, the configuration becomes:

# config/packages/framework.yaml
framework:
    session:
        handler_id: 'session.handler.native_file'
        save_path: '%kernel.project_dir%/var/sessions/%kernel.environment%'
        cookie_secure: auto
        cookie_samesite: lax
        storage_factory_id: session.storage.factory.native

Now, garbage collection does not work anymore and session files are stacking in the target folder. Yet, I thought that Symfony would have made a call to SessionHandler::gc.

Does this mean that Symfony does not support session cleaning by itself?

David
  • 2,603
  • 4
  • 18
  • 28
  • According to [this](https://stackoverflow.com/a/654341/231316), Debian cleans up sessions with a cron task and apparently has hard-coded paths. Not sure if that is still the case, but if so you could change the path and/or duplicate the task. – Chris Haas Jan 09 '23 at 17:19
  • I can't tell you the Symfony internals, if any, but Debian fiddles with `session.gc_probability` and `session.gc_divisor`. You need to restore them to sensible values. – Álvaro González Jan 10 '23 at 14:20

1 Answers1

0

https://www.php.net/manual/en/session.configuration.php#ini.session.save-path

Also note that if N is used and greater than 0 then automatic garbage collection will not be performed, see a copy of php.ini for further information.

I would check if $savePath in https://github.com/symfony/symfony/blob/6.3/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NativeFileSessionHandler.php file, is not prepended by N value.

TaurusHORN
  • 66
  • 8