4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpFoundation\Session\Storage;
14 use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
15 use Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeSessionHandler;
16 use Symfony\Component\HttpFoundation\Session\Storage\Proxy\AbstractProxy;
17 use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy;
20 * This provides a base class for session attribute storage.
22 * @author Drak <drak@zikula.org>
24 class NativeSessionStorage implements SessionStorageInterface
27 * Array of SessionBagInterface.
29 * @var SessionBagInterface[]
36 protected $started = false;
41 protected $closed = false;
46 protected $saveHandler;
51 protected $metadataBag;
56 * Depending on how you want the storage driver to behave you probably
57 * want to override this constructor entirely.
59 * List of options for $options array with their defaults.
61 * @see http://php.net/session.configuration for options
62 * but we omit 'session.' from the beginning of the keys for convenience.
64 * ("auto_start", is not supported as it tells PHP to start a session before
65 * PHP starts to execute user-land code. Setting during runtime has no effect).
67 * cache_limiter, "" (use "0" to prevent headers from being sent entirely).
70 * cookie_lifetime, "0"
76 * gc_maxlifetime, "1440"
78 * hash_bits_per_character, "4"
82 * serialize_handler, "php"
83 * use_strict_mode, "0"
85 * use_only_cookies, "1"
87 * upload_progress.enabled, "1"
88 * upload_progress.cleanup, "1"
89 * upload_progress.prefix, "upload_progress_"
90 * upload_progress.name, "PHP_SESSION_UPLOAD_PROGRESS"
91 * upload_progress.freq, "1%"
92 * upload_progress.min-freq, "1"
93 * url_rewriter.tags, "a=href,area=href,frame=src,form=,fieldset="
95 * sid_bits_per_character, "5"
96 * trans_sid_hosts, $_SERVER['HTTP_HOST']
97 * trans_sid_tags, "a=href,area=href,frame=src,form="
99 * @param array $options Session configuration options
100 * @param AbstractProxy|NativeSessionHandler|\SessionHandlerInterface|null $handler
101 * @param MetadataBag $metaBag MetadataBag
103 public function __construct(array $options = array(), $handler = null, MetadataBag $metaBag = null)
105 session_cache_limiter(''); // disable by default because it's managed by HeaderBag (if used)
106 ini_set('session.use_cookies', 1);
108 session_register_shutdown();
110 $this->setMetadataBag($metaBag);
111 $this->setOptions($options);
112 $this->setSaveHandler($handler);
116 * Gets the save handler instance.
118 * @return AbstractProxy
120 public function getSaveHandler()
122 return $this->saveHandler;
128 public function start()
130 if ($this->started) {
134 if (\PHP_SESSION_ACTIVE === session_status()) {
135 throw new \RuntimeException('Failed to start the session: already started by PHP.');
138 if (ini_get('session.use_cookies') && headers_sent($file, $line)) {
139 throw new \RuntimeException(sprintf('Failed to start the session because headers have already been sent by "%s" at line %d.', $file, $line));
142 // ok to try and start the session
143 if (!session_start()) {
144 throw new \RuntimeException('Failed to start the session');
147 $this->loadSession();
155 public function getId()
157 return $this->saveHandler->getId();
163 public function setId($id)
165 $this->saveHandler->setId($id);
171 public function getName()
173 return $this->saveHandler->getName();
179 public function setName($name)
181 $this->saveHandler->setName($name);
187 public function regenerate($destroy = false, $lifetime = null)
189 // Cannot regenerate the session ID for non-active sessions.
190 if (\PHP_SESSION_ACTIVE !== session_status()) {
194 if (null !== $lifetime) {
195 ini_set('session.cookie_lifetime', $lifetime);
199 $this->metadataBag->stampNew();
202 $isRegenerated = session_regenerate_id($destroy);
204 // The reference to $_SESSION in session bags is lost in PHP7 and we need to re-create it.
205 // @see https://bugs.php.net/bug.php?id=70013
206 $this->loadSession();
208 return $isRegenerated;
214 public function save()
216 session_write_close();
218 $this->closed = true;
219 $this->started = false;
225 public function clear()
227 // clear out the bags
228 foreach ($this->bags as $bag) {
232 // clear out the session
235 // reconnect the bags to the session
236 $this->loadSession();
242 public function registerBag(SessionBagInterface $bag)
244 if ($this->started) {
245 throw new \LogicException('Cannot register a bag when the session is already started.');
248 $this->bags[$bag->getName()] = $bag;
254 public function getBag($name)
256 if (!isset($this->bags[$name])) {
257 throw new \InvalidArgumentException(sprintf('The SessionBagInterface %s is not registered.', $name));
260 if ($this->saveHandler->isActive() && !$this->started) {
261 $this->loadSession();
262 } elseif (!$this->started) {
266 return $this->bags[$name];
270 * Sets the MetadataBag.
272 * @param MetadataBag $metaBag
274 public function setMetadataBag(MetadataBag $metaBag = null)
276 if (null === $metaBag) {
277 $metaBag = new MetadataBag();
280 $this->metadataBag = $metaBag;
284 * Gets the MetadataBag.
286 * @return MetadataBag
288 public function getMetadataBag()
290 return $this->metadataBag;
296 public function isStarted()
298 return $this->started;
302 * Sets session.* ini variables.
304 * For convenience we omit 'session.' from the beginning of the keys.
305 * Explicitly ignores other ini keys.
307 * @param array $options Session ini directives array(key => value)
309 * @see http://php.net/session.configuration
311 public function setOptions(array $options)
313 $validOptions = array_flip(array(
314 'cache_limiter', 'cookie_domain', 'cookie_httponly',
315 'cookie_lifetime', 'cookie_path', 'cookie_secure',
316 'entropy_file', 'entropy_length', 'gc_divisor',
317 'gc_maxlifetime', 'gc_probability', 'hash_bits_per_character',
318 'hash_function', 'name', 'referer_check',
319 'serialize_handler', 'use_strict_mode', 'use_cookies',
320 'use_only_cookies', 'use_trans_sid', 'upload_progress.enabled',
321 'upload_progress.cleanup', 'upload_progress.prefix', 'upload_progress.name',
322 'upload_progress.freq', 'upload_progress.min-freq', 'url_rewriter.tags',
323 'sid_length', 'sid_bits_per_character', 'trans_sid_hosts', 'trans_sid_tags',
326 foreach ($options as $key => $value) {
327 if (isset($validOptions[$key])) {
328 ini_set('session.'.$key, $value);
334 * Registers session save handler as a PHP session handler.
336 * To use internal PHP session save handlers, override this method using ini_set with
337 * session.save_handler and session.save_path e.g.
339 * ini_set('session.save_handler', 'files');
340 * ini_set('session.save_path', '/tmp');
342 * or pass in a NativeSessionHandler instance which configures session.save_handler in the
343 * constructor, for a template see NativeFileSessionHandler or use handlers in
344 * composer package drak/native-session
346 * @see http://php.net/session-set-save-handler
347 * @see http://php.net/sessionhandlerinterface
348 * @see http://php.net/sessionhandler
349 * @see http://github.com/drak/NativeSession
351 * @param AbstractProxy|NativeSessionHandler|\SessionHandlerInterface|null $saveHandler
353 * @throws \InvalidArgumentException
355 public function setSaveHandler($saveHandler = null)
357 if (!$saveHandler instanceof AbstractProxy &&
358 !$saveHandler instanceof NativeSessionHandler &&
359 !$saveHandler instanceof \SessionHandlerInterface &&
360 null !== $saveHandler) {
361 throw new \InvalidArgumentException('Must be instance of AbstractProxy or NativeSessionHandler; implement \SessionHandlerInterface; or be null.');
364 // Wrap $saveHandler in proxy and prevent double wrapping of proxy
365 if (!$saveHandler instanceof AbstractProxy && $saveHandler instanceof \SessionHandlerInterface) {
366 $saveHandler = new SessionHandlerProxy($saveHandler);
367 } elseif (!$saveHandler instanceof AbstractProxy) {
368 $saveHandler = new SessionHandlerProxy(new \SessionHandler());
370 $this->saveHandler = $saveHandler;
372 if ($this->saveHandler instanceof \SessionHandlerInterface) {
373 session_set_save_handler($this->saveHandler, false);
378 * Load the session with attributes.
380 * After starting the session, PHP retrieves the session from whatever handlers
381 * are set to (either PHP's internal, or a custom save handler set with session_set_save_handler()).
382 * PHP takes the return value from the read() handler, unserializes it
383 * and populates $_SESSION with the result automatically.
385 * @param array|null $session
387 protected function loadSession(array &$session = null)
389 if (null === $session) {
390 $session = &$_SESSION;
393 $bags = array_merge($this->bags, array($this->metadataBag));
395 foreach ($bags as $bag) {
396 $key = $bag->getStorageKey();
397 $session[$key] = isset($session[$key]) ? $session[$key] : array();
398 $bag->initialize($session[$key]);
401 $this->started = true;
402 $this->closed = false;