Skip to content

Commit 6990711

Browse files
committed
Merge pull request symfony#2562 from drak/proxy_session
[Cookbook] Added some examples of session save handler proxies
2 parents c108fe9 + 486ceb5 commit 6990711

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

cookbook/session/index.rst

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Sessions
2+
========
3+
4+
.. toctree::
5+
:maxdepth: 2
6+
7+
proxy_examples

cookbook/session/proxy_examples.rst

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
.. index::
2+
single: Sessions, session proxy, proxy
3+
4+
Session Proxy Examples
5+
----------------------
6+
7+
The session proxy mechanism has a variety of uses, this
8+
example demonstrates two common uses. Rather than injecting
9+
the session handler as normal, a handler is injected into the proxy
10+
and registered with the session storage driver::
11+
12+
<?php
13+
use Symfony\Component\HttpFoundation\Session\Session;
14+
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
15+
use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionStorage;
16+
17+
$proxy = new YourProxy(new PdoSessionStorage());
18+
$session = new Session(new NativeSessionStorage($proxy));
19+
20+
The examples below show elaborate on two use-cases.
21+
22+
Encryption of Session Data
23+
--------------------------
24+
25+
If you wanted to encrypt the session data you could use the proxy to encrypt
26+
and decrypt the session as required::
27+
28+
<?php
29+
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy;
30+
31+
class EncryptedSessionProxy extends SessionHandlerProxy
32+
{
33+
private $key;
34+
35+
public function __construct(\SessionHandlerInterface $handler, $key)
36+
{
37+
$this->key = $key;
38+
39+
parent::__construct($handler);
40+
}
41+
42+
public function read($id)
43+
{
44+
$data = parent::write($id, $data);
45+
46+
return mcrypt_decrypt(\MCRYPT_3DES, $this->key, $data);
47+
}
48+
49+
public function write($id, $data)
50+
{
51+
$data = mcrypt_encrypt(\MCRYPT_3DES, $this->key, $data);
52+
53+
return parent::write($id, $data);
54+
}
55+
}
56+
57+
58+
Readonly Guest sessions
59+
-----------------------
60+
61+
There are some applications where a session is required for guest
62+
users, but there is no particular need persist the session. In this
63+
case you can intercept the session before it writes::
64+
65+
<?php
66+
use Foo\User;
67+
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy;
68+
69+
class ReadOnlyGuestSessionProxy extends SessionHandlerProxy
70+
{
71+
private $user;
72+
73+
public function __construct(\SessionHandlerInterface $handler, User $user)
74+
{
75+
$this->user = $user;
76+
77+
parent::__construct($handler);
78+
}
79+
80+
public function write($id, $data)
81+
{
82+
if ($this->user->isGuest()) {
83+
return;
84+
}
85+
86+
return parent::write($id, $data);
87+
}
88+
}
89+

0 commit comments

Comments
 (0)