Skip to content

Add page about configuring the session TTL #16880

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions session.rst
Original file line number Diff line number Diff line change
Expand Up @@ -192,5 +192,6 @@ More about Sessions
session/locale_sticky_session
session/php_bridge
session/proxy_examples
session/configuring_ttl

.. _`HttpFoundation component`: https://symfony.com/components/HttpFoundation
121 changes: 121 additions & 0 deletions session/configuring_ttl.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
.. index::
single: Sessions, defining TTL

Configuring the Session TTL
===========================

Symfony by default will use PHP's ini setting ``session.gc_maxlifetime`` as
session lifetime. However if you :doc:`store sessions in a database </session/database>`
you can also configure your own TTL in the framework configuration or even at runtime.

Changing the ini setting is not possible once the session is started so if you
want to use a different TTL depending on which user is logged in, you really need
to do it at runtime using the callback method below.

.. _configuring-the-TTL:

Configuring the TTL
-------------------

You need to pass the TTL in the options array of the session handler you are using:

.. configuration-block::

.. code-block:: yaml

# config/services.yaml
services:
# ...
Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler:
arguments:
- '@Redis'
- { 'ttl': 600 }

.. code-block:: xml

<!-- config/services.xml -->
<services>
<service id="Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler">
<argument type="service" id="Redis"/>
<argument type="collection">
<argument key="ttl">600</argument>
</argument>
</service>
</services>

.. code-block:: php

// config/services.php
use Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler;

$services
->set(RedisSessionHandler::class)
->args([
service('Redis'),
['ttl' => 600],
]);

.. _configuring-the-TTL-dynamically-at-runtime:

Configuring the TTL dynamically at runtime
------------------------------------------

If you would like to have a different TTL for different
users or sessions for whatever reason, this is also possible
by passing a callback as the TTL value. The callback then has
to return an integer which will be used as TTL.

The callback will be called right before the session is written.

.. configuration-block::

.. code-block:: yaml

# config/services.yaml
services:
# ...
Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler:
arguments:
- '@Redis'
- { 'ttl': !closure '@my.ttl.handler' }

my.ttl.handler:
class: Some\InvokableClass # some class with an __invoke() method
arguments:
# Inject whatever dependencies you need to be able to resolve a TTL for the current session
- '@security'

.. code-block:: xml

<!-- config/services.xml -->
<services>
<service id="Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler">
<argument type="service" id="Redis"/>
<argument type="collection">
<argument key="ttl" type="closure" id="my.ttl.handler"/>
</argument>
</service>
<!-- some class with an __invoke() method -->
<service id="my.ttl.handler" class="Some\InvokableClass">
<!-- Inject whatever dependencies you need to be able to resolve a TTL for the current session -->
<argument type="service" id="security"/>
</service>
</services>

.. code-block:: php

// config/services.php
use Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler;

$services
->set(RedisSessionHandler::class)
->args([
service('Redis'),
['ttl' => closure(service('my.ttl.handler'))],
]);

$services
// some class with an __invoke() method
->set('my.ttl.handler', 'Some\InvokableClass')
// Inject whatever dependencies you need to be able to resolve a TTL for the current session
->args([service('security')]);