session.class.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. namespace System;
  4. /**
  5. * Session Management.
  6. *
  7. * @see ChamiloSession
  8. *
  9. * @license see /license.txt
  10. * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva
  11. */
  12. class Session implements \ArrayAccess
  13. {
  14. /**
  15. * @param string $name
  16. */
  17. public function __unset($name)
  18. {
  19. unset($_SESSION[$name]);
  20. }
  21. /**
  22. * @param string $name
  23. *
  24. * @return bool
  25. */
  26. public function __isset($name)
  27. {
  28. return self::has($name);
  29. }
  30. /**
  31. * It it exists returns the value stored at the specified offset.
  32. * If offset does not exists returns null. Do not trigger a warning.
  33. *
  34. * @param string $name
  35. *
  36. * @return mixed
  37. */
  38. public function __get($name)
  39. {
  40. return self::read($name);
  41. }
  42. /**
  43. * @param string $name
  44. * @param mixed $value
  45. */
  46. public function __set($name, $value)
  47. {
  48. self::write($name, $value);
  49. }
  50. /**
  51. * @param string $variable
  52. * @param null $default
  53. *
  54. * @return mixed
  55. */
  56. public static function read($variable, $default = null)
  57. {
  58. return isset($_SESSION[$variable]) ? $_SESSION[$variable] : $default;
  59. }
  60. /**
  61. * @param string $variable
  62. * @param mixed $value
  63. */
  64. public static function write($variable, $value)
  65. {
  66. $_SESSION[$variable] = $value;
  67. }
  68. /**
  69. * @param string $variable
  70. */
  71. public static function erase($variable)
  72. {
  73. $variable = (string) $variable;
  74. if (isset($GLOBALS[$variable])) {
  75. unset($GLOBALS[$variable]);
  76. }
  77. if (isset($_SESSION[$variable])) {
  78. unset($_SESSION[$variable]);
  79. }
  80. }
  81. /**
  82. * Returns true if session has variable set up, false otherwise.
  83. *
  84. * @param string $variable
  85. *
  86. * @return bool
  87. */
  88. public static function has($variable)
  89. {
  90. return isset($_SESSION[$variable]);
  91. }
  92. /**
  93. * Clear session.
  94. */
  95. public static function clear()
  96. {
  97. session_regenerate_id();
  98. session_unset();
  99. $_SESSION = [];
  100. }
  101. /**
  102. * Destroy session.
  103. */
  104. public static function destroy()
  105. {
  106. session_unset();
  107. $_SESSION = [];
  108. session_destroy();
  109. }
  110. /*
  111. * ArrayAccess
  112. */
  113. public function offsetExists($offset)
  114. {
  115. return isset($_SESSION[$offset]);
  116. }
  117. /**
  118. * It it exists returns the value stored at the specified offset.
  119. * If offset does not exists returns null. Do not trigger a warning.
  120. *
  121. * @param string $offset
  122. *
  123. * @return mixed
  124. */
  125. public function offsetGet($offset)
  126. {
  127. return self::read($offset);
  128. }
  129. public function offsetSet($offset, $value)
  130. {
  131. self::write($offset, $value);
  132. }
  133. public function offsetUnset($offset)
  134. {
  135. unset($_SESSION[$offset]);
  136. }
  137. }