Session Proxy Examples

Session Proxy Examples

The session proxy mechanism has a variety of uses and this article demonstrates two common uses. Rather than using the regular session handler, you can create a custom save handler by defining a class that extends the Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy class.

Then, define the class as a service. If you’re using the default services.yaml configuration, that happens automatically.

Finally, use the framework.session.handler_id configuration option to tell Symfony to use your session handler instead of the default one:

  • YAML

    1. # config/packages/framework.yaml
    2. framework:
    3. session:
    4. # ...
    5. handler_id: App\Session\CustomSessionHandler
  • XML

    1. <!-- config/packages/framework.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:framework="http://symfony.com/schema/dic/symfony"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services
    7. https://symfony.com/schema/dic/services/services-1.0.xsd">
    8. <framework:config>
    9. <framework:session handler-id="App\Session\CustomSessionHandler"/>
    10. </framework:config>
    11. </container>
  • PHP

    1. // config/packages/framework.php
    2. use App\Session\CustomSessionHandler;
    3. $container->loadFromExtension('framework', [
    4. // ...
    5. 'session' => [
    6. // ...
    7. 'handler_id' => CustomSessionHandler::class,
    8. ],
    9. ]);

Keep reading the next sections to learn how to use the session handlers in practice to solve two common use cases: encrypt session information and define read-only guest sessions.

Encryption of Session Data

If you want to encrypt the session data, you can use the proxy to encrypt and decrypt the session as required. The following example uses the php-encryption library, but you can adapt it to any other library that you may be using:

  1. // src/Session/EncryptedSessionProxy.php
  2. namespace App\Session;
  3. use Defuse\Crypto\Crypto;
  4. use Defuse\Crypto\Key;
  5. use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy;
  6. class EncryptedSessionProxy extends SessionHandlerProxy
  7. {
  8. private $key;
  9. public function __construct(\SessionHandlerInterface $handler, Key $key)
  10. {
  11. $this->key = $key;
  12. parent::__construct($handler);
  13. }
  14. public function read($id)
  15. {
  16. $data = parent::read($id);
  17. return Crypto::decrypt($data, $this->key);
  18. }
  19. public function write($id, $data)
  20. {
  21. $data = Crypto::encrypt($data, $this->key);
  22. return parent::write($id, $data);
  23. }
  24. }

Read-only Guest Sessions

There are some applications where a session is required for guest users, but where there is no particular need to persist the session. In this case you can intercept the session before it is written:

  1. // src/Session/ReadOnlySessionProxy.php
  2. namespace App\Session;
  3. use App\Entity\User;
  4. use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy;
  5. use Symfony\Component\Security\Core\Security;
  6. class ReadOnlySessionProxy extends SessionHandlerProxy
  7. {
  8. private $security;
  9. public function __construct(\SessionHandlerInterface $handler, Security $security)
  10. {
  11. $this->security = $security;
  12. parent::__construct($handler);
  13. }
  14. public function write($id, $data)
  15. {
  16. if ($this->getUser() && $this->getUser()->isGuest()) {
  17. return;
  18. }
  19. return parent::write($id, $data);
  20. }
  21. private function getUser()
  22. {
  23. $user = $this->security->getUser();
  24. if (is_object($user)) {
  25. return $user;
  26. }
  27. }
  28. }

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.