sfSessionStorage.class.php 5.1 KB

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