1. sfSessionStorage.class.php
  2. /** * sfSessionStorage allows you to store persistent symfony data in the user session. * * Optional parameters: * * # auto_start - [Yes] - Should session_start() automatically be called? * # session_name - [symfony] - The name of the session. * * @package symfony * @subpackage storage * @author Fabien Potencier * @author Sean Kerr * @version SVN: $Id: sfSessionStorage.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $ */
  3. class sfSessionStorage extends sfStorage
  4. {
  5. static protected
  6. $sessionIdRegenerated = false,
  7. $sessionStarted = false;
  8. /**
  9. * Available options:
  10. *
  11. * * session_name: The cookie name (symfony by default)
  12. * * session_id: The session id (null by default)
  13. * * auto_start: Whether to start the session (true by default)
  14. * * session_cookie_lifetime: Cookie lifetime
  15. * * session_cookie_path: Cookie path
  16. * * session_cookie_domain: Cookie domain
  17. * * session_cookie_secure: Cookie secure
  18. * * session_cookie_httponly: Cookie http only (only for PHP >= 5.2)
  19. *
  20. * The default values for all 'session_cookie_*' options are those returned by the session_get_cookie_params() function
  21. *
  22. * @param array $options An associative array of options
  23. *
  24. * @see sfStorage
  25. */
  26. public function initialize($options = null)
  27. {
  28. $cookieDefaults = session_get_cookie_params();
  29. $options = array_merge(array(
  30. 'session_name' => 'symfony',
  31. 'session_id' => null,
  32. 'auto_start' => true,
  33. 'session_cookie_lifetime' => $cookieDefaults['lifetime'],
  34. 'session_cookie_path' => $cookieDefaults['path'],
  35. 'session_cookie_domain' => $cookieDefaults['domain'],
  36. 'session_cookie_secure' => $cookieDefaults['secure'],
  37. 'session_cookie_httponly' => isset($cookieDefaults['httponly']) ? $cookieDefaults['httponly'] : false,
  38. 'session_cache_limiter' => 'none',
  39. ), $options);
  40. // initialize parent
  41. parent::initialize($options);
  42. // set session name
  43. $sessionName = $this->options['session_name'];
  44. session_name($sessionName);
  45. if (!(boolean) ini_get('session.use_cookies') && $sessionId = $this->options['session_id'])
  46. {
  47. session_id($sessionId);
  48. }
  49. $lifetime = $this->options['session_cookie_lifetime'];
  50. $path = $this->options['session_cookie_path'];
  51. $domain = $this->options['session_cookie_domain'];
  52. $secure = $this->options['session_cookie_secure'];
  53. $httpOnly = $this->options['session_cookie_httponly'];
  54. session_set_cookie_params($lifetime, $path, $domain, $secure, $httpOnly);
  55. if (null !== $this->options['session_cache_limiter'])
  56. {
  57. session_cache_limiter($this->options['session_cache_limiter']);
  58. }
  59. if ($this->options['auto_start'] && !self::$sessionStarted)
  60. {
  61. session_start();
  62. self::$sessionStarted = true;
  63. }
  64. }
  65. /**
  66. * Reads data from this storage.
  67. *
  68. * The preferred format for a key is directory style so naming conflicts can be avoided.
  69. *
  70. * @param string $key A unique key identifying your data
  71. *
  72. * @return mixed Data associated with the key
  73. */
  74. public function read($key)
  75. {
  76. $retval = null;
  77. if (isset($_SESSION[$key]))
  78. {
  79. $retval = $_SESSION[$key];
  80. }
  81. return $retval;
  82. }
  83. /**
  84. * Removes data from this storage.
  85. *
  86. * The preferred format for a key is directory style so naming conflicts can be avoided.
  87. *
  88. * @param string $key A unique key identifying your data
  89. *
  90. * @return mixed Data associated with the key
  91. */
  92. public function remove($key)
  93. {
  94. $retval = null;
  95. if (isset($_SESSION[$key]))
  96. {
  97. $retval = $_SESSION[$key];
  98. unset($_SESSION[$key]);
  99. }
  100. return $retval;
  101. }
  102. /**
  103. * Writes data to this storage.
  104. *
  105. * The preferred format for a key is directory style so naming conflicts can be avoided.
  106. *
  107. * @param string $key A unique key identifying your data
  108. * @param mixed $data Data associated with your key
  109. *
  110. */
  111. public function write($key, $data)
  112. {
  113. $_SESSION[$key] = $data;
  114. }
  115. /**
  116. * Regenerates id that represents this storage.
  117. *
  118. * @param boolean $destroy Destroy session when regenerating?
  119. *
  120. * @return boolean True if session regenerated, false if error
  121. *
  122. */
  123. public function regenerate($destroy = false)
  124. {
  125. if (self::$sessionIdRegenerated)
  126. {
  127. return;
  128. }
  129. // regenerate a new session id once per object
  130. session_regenerate_id($destroy);
  131. self::$sessionIdRegenerated = true;
  132. }
  133. /**
  134. * Executes the shutdown procedure.
  135. *
  136. */
  137. public function shutdown()
  138. {
  139. // don't need a shutdown procedure because read/write do it in real-time
  140. session_write_close();
  141. }
  142. }

Debug toolbar