You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

202 lines
5.0 KiB

  1. <?php
  2. /**
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage session
  7. * @since 2010-03-14
  8. * @version SVN: $Id$
  9. * @filesource $URL$
  10. */
  11. class Session
  12. {
  13. /**
  14. * Default number of seconds the session will be remembered
  15. * for when asked to be remembered
  16. *
  17. * @var int
  18. */
  19. private static $remember_time = 1209600; // 2 weeks
  20. private static $started = false;
  21. /**
  22. * Avoid new
  23. */
  24. private function __construct(){}
  25. /**
  26. * Avoid cloning
  27. */
  28. private function __clone(){}
  29. /**
  30. * Retrieve a member of the $_SESSION
  31. *
  32. * If no $key is passed, returns the entire $_SESSION array.
  33. *
  34. * @param string $key
  35. * @param mixed $default Default value to use if key not found
  36. * @return mixed Returns null if key does not exist
  37. */
  38. public static function get($key = null, $default = null)
  39. {
  40. if (null === $key) {
  41. return $_SESSION;
  42. }
  43. return isset($_SESSION[$key]) ? $_SESSION[$key] : $default;
  44. }
  45. /**
  46. * Set $_SESSION values
  47. *
  48. * @param string|array $spec
  49. * @param null|mixed $value
  50. * @return void
  51. */
  52. public static function set($spec, $value = null)
  53. {
  54. if (is_array($spec)) {
  55. foreach ($spec as $key => $value) {
  56. self::set($key, $value);
  57. }
  58. return;
  59. }
  60. $_SESSION[(string) $spec] = $value;
  61. }
  62. /**
  63. * Delete Session value
  64. *
  65. * @param string $key
  66. */
  67. public static function del($key)
  68. {
  69. if (isset($_SESSION[$key])) {
  70. unset($_SESSION[$key]);
  71. }
  72. }
  73. /**
  74. * remember() - Write a persistent cookie that expires after a number
  75. * of seconds in the future. If no number of seconds is specified,
  76. * then this defaults to self::$rememberMeSeconds.
  77. *
  78. * @param $seconds integer - OPTIONAL specifies TTL for cookie in seconds from present time
  79. * @return void
  80. */
  81. public static function remember($seconds = null)
  82. {
  83. $seconds = (int) $seconds;
  84. $seconds = ($seconds > 0) ? $seconds : self::$remember_time;
  85. self::rememberUntil($seconds);
  86. }
  87. /**
  88. * forget() - Write a volatile session cookie, removing any persistent
  89. * cookie that may have existed. The session would end upon,
  90. * for example, termination of a web browser program.
  91. *
  92. * @return void
  93. */
  94. public static function forget()
  95. {
  96. self::rememberUntil();
  97. }
  98. /**
  99. * rememberUntil() - This method does the work of changing the state of the session
  100. * cookie and making sure that it gets resent to the browser via regenerateId()
  101. *
  102. * @param int $seconds
  103. * @return void
  104. */
  105. public static function rememberUntil($seconds = 0)
  106. {
  107. $params = session_get_cookie_params();
  108. session_set_cookie_params(
  109. $seconds,
  110. $params['path'],
  111. $params['domain'],
  112. $params['secure']
  113. );
  114. self::regenerateId();
  115. }
  116. /**
  117. * regenerateId() - Regenerate the session id. Best practice is to call this after
  118. * session is started. If called prior to session starting, session id will be regenerated
  119. * at start time.
  120. *
  121. * @throws Zend_Session_Exception
  122. * @return void
  123. */
  124. public static function regenerateId()
  125. {
  126. if (self::$started) {
  127. session_regenerate_id(true);
  128. }
  129. }
  130. public static function isExists()
  131. {
  132. if (ini_get('session.use_cookies') == '1' && isset($COOKIE[session_name()])) {
  133. return true;
  134. }
  135. return false;
  136. }
  137. public static function start()
  138. {
  139. self::$started = true;
  140. session_start();
  141. self::regenerateId();
  142. }
  143. /**
  144. * getId() - get the current session id
  145. *
  146. * @return string
  147. */
  148. public static function getId()
  149. {
  150. return session_id();
  151. }
  152. /**
  153. * destroy() - This is used to destroy session data, and optionally,
  154. * the session cookie itself
  155. *
  156. * @param bool $remove_cookie - OPTIONAL remove session id cookie,
  157. * defaults to true (remove cookie)
  158. * @return void
  159. */
  160. public static function destroy($remove_cookie = true)
  161. {
  162. session_destroy();
  163. if ($remove_cookie) {
  164. self::expireSessionCookie();
  165. }
  166. }
  167. /**
  168. * expireSessionCookie() - Sends an expired session id cookie, causing the client to delete the session cookie
  169. *
  170. * @return void
  171. */
  172. public static function expireSessionCookie()
  173. {
  174. if (isset($COOKIE[session_name()])) {
  175. $params = session_get_cookie_params();
  176. setcookie(
  177. session_name(),
  178. false,
  179. 0,
  180. $params['path'],
  181. $params['domain'],
  182. $params['secure']
  183. );
  184. }
  185. }
  186. }