chamilo_session.class.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. use Chamilo\CoreBundle\Framework\Container;
  3. /**
  4. * ChamiloSession class
  5. */
  6. class ChamiloSession
  7. {
  8. /**
  9. * @param $variable
  10. * @param null $default
  11. * @return null
  12. */
  13. public static function read($variable, $default = null)
  14. {
  15. $session = Container::getSession();
  16. $result = null;
  17. if (isset($session)) {
  18. $result = $session->get($variable);
  19. }
  20. // Check if the value exists in the $_SESSION array
  21. if (empty($result)) {
  22. return isset($_SESSION[$variable]) ? $_SESSION[$variable] : $default;
  23. } else {
  24. return $result;
  25. }
  26. }
  27. /**
  28. * @param $variable
  29. * @param $value
  30. */
  31. public static function write($variable, $value)
  32. {
  33. $session = Container::getSession();
  34. // Writing the session in 2 instances because
  35. $_SESSION[$variable] = $value;
  36. $session->set($variable, $value);
  37. }
  38. /**
  39. * @param $variable
  40. */
  41. public static function erase($variable)
  42. {
  43. $variable = (string) $variable;
  44. $session = Container::getSession();
  45. $session->remove($variable);
  46. if (isset($GLOBALS[$variable])) {
  47. unset($GLOBALS[$variable]);
  48. }
  49. if (isset($_SESSION[$variable])) {
  50. unset($_SESSION[$variable]);
  51. }
  52. }
  53. /**
  54. * Clear session
  55. */
  56. public static function clear()
  57. {
  58. $session = Container::getSession();
  59. $session->clear();
  60. }
  61. /**
  62. * Invalidates a session
  63. */
  64. public static function destroy()
  65. {
  66. $session = Container::getSession();
  67. $session->invalidate();
  68. }
  69. }