Skip to content

Commit a37021d

Browse files
author
Drak
committed
Added some examples of session save handler proxies
1 parent d7e8a07 commit a37021d

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

cookbook/sessions/index.rst

Lines changed: 7 additions & 0 deletions
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/sessions/proxy_examples.rst

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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, in this
8+
example we'll demonstrate two common uses. Rather than injecting
9+
the session handler as normal, you inject the handler into the proxy
10+
and regsiter the proxy with the session::
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 example below show should give some ideas for ``YourProxy``.
21+
22+
23+
Encryption of Session Data
24+
--------------------------
25+
26+
If you wanted to encrypt the session data you could use the proxy to encrypt
27+
and decrypt the session as required::
28+
29+
<?php
30+
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy;
31+
32+
class NoGuestUserSessionProxy extends SessionHandlerProxy
33+
{
34+
private $key;
35+
36+
public function __construct(\SessionHandlerInterface $handler, $key)
37+
{
38+
$this->key = $key;
39+
40+
parent::__construct($handler);
41+
}
42+
43+
public function read($id)
44+
{
45+
$data = parent::write($id, $data);
46+
47+
return mcrypt_decrypt(\MCRYPT_3DES, $this->key, $data);
48+
}
49+
50+
public function write($id, $data)
51+
{
52+
$data = mcrypt_encrypt(\MCRYPT_3DES, $this->key, $data);
53+
54+
return parent::write($id, $data);
55+
}
56+
}
57+
58+
59+
Readonly Guest sessions
60+
-----------------------
61+
62+
There are some applications where a session is required for guest
63+
users, but there is no particular need persist the session. In this
64+
case you can intercept the session before it writes::
65+
66+
<?php
67+
use Foo\User;
68+
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy;
69+
70+
class ReadOnlyGuestSessionProxy extends SessionHandlerProxy
71+
{
72+
private $user;
73+
74+
public function __construct(\SessionHandlerInterface $handler, User $user)
75+
{
76+
$this->user = $user;
77+
78+
parent::__construct($handler);
79+
}
80+
81+
public function write($id, $data)
82+
{
83+
if ($this->user->isGuest()) {
84+
return;
85+
}
86+
87+
return parent::write($id, $data);
88+
}
89+
}
90+

0 commit comments

Comments
 (0)