session.class.php 2.4 KB

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