PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/symfony/lib/storage/sfSessionStorage.class.php

https://bitbucket.org/suntiser/mfec_php_test
PHP | 187 lines | 80 code | 22 blank | 85 comment | 8 complexity | 22a747741273eb3849961c191ed08c80 MD5 | raw file
Possible License(s): LGPL-3.0
  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$
  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. * @return void
  48. */
  49. public function initialize($options = null)
  50. {
  51. $cookieDefaults = session_get_cookie_params();
  52. $options = array_merge(array(
  53. 'session_name' => 'symfony',
  54. 'session_id' => null,
  55. 'auto_start' => true,
  56. 'session_cookie_lifetime' => $cookieDefaults['lifetime'],
  57. 'session_cookie_path' => $cookieDefaults['path'],
  58. 'session_cookie_domain' => $cookieDefaults['domain'],
  59. 'session_cookie_secure' => $cookieDefaults['secure'],
  60. 'session_cookie_httponly' => isset($cookieDefaults['httponly']) ? $cookieDefaults['httponly'] : false,
  61. 'session_cache_limiter' => null,
  62. ), $options);
  63. // initialize parent
  64. parent::initialize($options);
  65. // set session name
  66. $sessionName = $this->options['session_name'];
  67. session_name($sessionName);
  68. if (!(boolean) ini_get('session.use_cookies') && $sessionId = $this->options['session_id'])
  69. {
  70. session_id($sessionId);
  71. }
  72. $lifetime = $this->options['session_cookie_lifetime'];
  73. $path = $this->options['session_cookie_path'];
  74. $domain = $this->options['session_cookie_domain'];
  75. $secure = $this->options['session_cookie_secure'];
  76. $httpOnly = $this->options['session_cookie_httponly'];
  77. session_set_cookie_params($lifetime, $path, $domain, $secure, $httpOnly);
  78. if (null !== $this->options['session_cache_limiter'])
  79. {
  80. session_cache_limiter($this->options['session_cache_limiter']);
  81. }
  82. if ($this->options['auto_start'] && !self::$sessionStarted)
  83. {
  84. session_start();
  85. self::$sessionStarted = true;
  86. }
  87. }
  88. /**
  89. * Reads data from this storage.
  90. *
  91. * The preferred format for a key is directory style so naming conflicts can be avoided.
  92. *
  93. * @param string $key A unique key identifying your data
  94. *
  95. * @return mixed Data associated with the key
  96. */
  97. public function read($key)
  98. {
  99. $retval = null;
  100. if (isset($_SESSION[$key]))
  101. {
  102. $retval = $_SESSION[$key];
  103. }
  104. return $retval;
  105. }
  106. /**
  107. * Removes data from this storage.
  108. *
  109. * The preferred format for a key is directory style so naming conflicts can be avoided.
  110. *
  111. * @param string $key A unique key identifying your data
  112. *
  113. * @return mixed Data associated with the key
  114. */
  115. public function remove($key)
  116. {
  117. $retval = null;
  118. if (isset($_SESSION[$key]))
  119. {
  120. $retval = $_SESSION[$key];
  121. unset($_SESSION[$key]);
  122. }
  123. return $retval;
  124. }
  125. /**
  126. * Writes data to this storage.
  127. *
  128. * The preferred format for a key is directory style so naming conflicts can be avoided.
  129. *
  130. * @param string $key A unique key identifying your data
  131. * @param mixed $data Data associated with your key
  132. *
  133. */
  134. public function write($key, $data)
  135. {
  136. $_SESSION[$key] = $data;
  137. }
  138. /**
  139. * Regenerates id that represents this storage.
  140. *
  141. * @param boolean $destroy Destroy session when regenerating?
  142. *
  143. * @return bool|void
  144. *
  145. */
  146. public function regenerate($destroy = false)
  147. {
  148. if (self::$sessionIdRegenerated)
  149. {
  150. return;
  151. }
  152. // regenerate a new session id once per object
  153. session_regenerate_id($destroy);
  154. self::$sessionIdRegenerated = true;
  155. }
  156. /**
  157. * Executes the shutdown procedure.
  158. *
  159. */
  160. public function shutdown()
  161. {
  162. // don't need a shutdown procedure because read/write do it in real-time
  163. session_write_close();
  164. }
  165. }