session.class.php 2.9 KB

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