session.class.php 2.5 KB

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