SaveSessionListener

class SaveSessionListener implements EventSubscriberInterface

Saves the session, in case it is still open, before sending the response/headers.

This ensures several things in case the developer did not save the session explicitly:

  • If a session save handler without locking is used, it ensures the data is available on the next request, e.g. after a redirect. PHPs auto-save at script end via sessionregistershutdown is executed after fastcgifinishrequest. So in this case the data could be missing the next request because it might not be saved the moment the new request is processed.
  • A locking save handler (e.g. the native 'files') circumvents concurrency problems like the one above. But by saving the session before long-running things in the terminate event, we ensure the session is not blocked longer than needed.
  • When regenerating the session ID no locking is involved in PHPs session design. See https://bugs.php.net/bug.php?id=61470 for a discussion. So in this case, the session must be saved anyway before sending the headers with the new session ID. Otherwise session data could get lost again for concurrent requests with the new ID. One result could be that you get logged out after just logging in.

This listener should be executed as one of the last listeners, so that previous listeners can still operate on the open session. This prevents the overhead of restarting it. Listeners after closing the session can still work with the session as usual because Symfonys session implementation starts the session on demand. So writing to it after it is saved will just restart it.

Methods

onKernelResponse(FilterResponseEvent $event)
static array getSubscribedEvents()

Returns an array of event names this subscriber wants to listen to.

Details

onKernelResponse(FilterResponseEvent $event)

Parameters

FilterResponseEvent $event

static array getSubscribedEvents()

Returns an array of event names this subscriber wants to listen to.

The array keys are event names and the value can be:

  • The method name to call (priority defaults to 0)
  • An array composed of the method name to call and the priority
  • An array of arrays composed of the method names to call and respective priorities, or 0 if unset

For instance:

  • array('eventName' => 'methodName')
  • array('eventName' => array('methodName', $priority))
  • array('eventName' => array(array('methodName1', $priority), array('methodName2'))

Return Value

array The event names to listen to

© 2004–2017 Fabien Potencier
Licensed under the MIT License.
http://api.symfony.com/2.8/Symfony/Component/HttpKernel/EventListener/SaveSessionListener.html