Session.Class.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace Mall\Framework\Core;
  3. use Mall\Framework\Session\SaveHandler\SaveHandlerInterface;
  4. use Mall\Framework\Session\SaveHandler\RedisSession;
  5. class Session
  6. {
  7. private static $_instance;
  8. protected static $defaultOptions = array(
  9. 'save_path' => null,
  10. 'name' => null, /* this should be set to a unique value for each application */
  11. 'save_handler' => null,
  12. //'auto_start' => null, /* intentionally excluded (see manual) */
  13. 'gc_probability' => null,
  14. 'gc_divisor' => null,
  15. 'gc_maxlifetime' => null,
  16. 'serialize_handler' => null,
  17. 'cookie_lifetime' => null,
  18. 'cookie_path' => null,
  19. 'cookie_domain' => null,
  20. 'cookie_secure' => null,
  21. 'cookie_httponly' => null,
  22. 'use_cookies' => null,
  23. 'use_only_cookies' => 'on',
  24. 'referer_check' => null,
  25. 'entropy_file' => null,
  26. 'entropy_length' => null,
  27. 'cache_limiter' => null,
  28. 'cache_expire' => null,
  29. 'use_trans_sid' => null,
  30. 'bug_compat_42' => null,
  31. 'bug_compat_warn' => null,
  32. 'hash_function' => null,
  33. 'hash_bits_per_character' => null
  34. );
  35. static public function getInstance()
  36. {
  37. $key = 'SessionRedis';
  38. if (!isset(self::$_instance[$key])) {
  39. $obj = new self();
  40. $obj->connect();
  41. self::$_instance[$key] = $obj;
  42. }
  43. return self::$_instance[$key];
  44. }
  45. public function connect()
  46. {
  47. $saveHandler = new RedisSession();
  48. $this->registerSaveHandler($saveHandler);
  49. session_start();
  50. }
  51. public static function setOptions($options)
  52. {
  53. if (!is_array($options)) {
  54. throw new \Exception(sprintf(
  55. 'Parameter provided to %s must be an array or Traversable',
  56. __METHOD__
  57. ));
  58. }
  59. // set the options the user has requested to set
  60. foreach ($options as $name => $value) {
  61. $name = strtolower($name);
  62. // set the ini based values
  63. if (array_key_exists($name, self::$defaultOptions)) {
  64. ini_set("session.$name", $value);
  65. }
  66. }
  67. }
  68. public function set($name, $value)
  69. {
  70. if (!self::sessionExists()) {
  71. return FALSE;
  72. }
  73. $_SESSION[$name] = $value;
  74. }
  75. public function get($name)
  76. {
  77. if (!self::sessionExists()) {
  78. return FALSE;
  79. }
  80. if (isset($_SESSION[$name])) {
  81. return $_SESSION[$name];
  82. }
  83. return FALSE;
  84. }
  85. /**
  86. * Does a session exist and is it currently active?
  87. *
  88. * @return bool
  89. */
  90. public static function sessionExists()
  91. {
  92. $sid = defined('SID') ? constant('SID') : false;
  93. if ($sid !== false && self::getId()) {
  94. return true;
  95. }
  96. if (headers_sent()) {
  97. return true;
  98. }
  99. return false;
  100. }
  101. /**
  102. * Set session ID
  103. *
  104. * @param string $id
  105. */
  106. public static function setId($id)
  107. {
  108. if (self::sessionExists()) {
  109. throw new \Exception (
  110. 'Session has already been started, to change the session ID call regenerateId()'
  111. );
  112. }
  113. session_id($id);
  114. }
  115. /**
  116. * Get session ID
  117. *
  118. * @return string
  119. */
  120. public static function getId()
  121. {
  122. return session_id();
  123. }
  124. /**
  125. * session destory
  126. */
  127. public function destory()
  128. {
  129. if (!self::sessionExists()) {
  130. return FALSE;
  131. }
  132. session_destroy();
  133. }
  134. /**
  135. * Register Save Handler with ext/session
  136. *
  137. * @param Mall\Framework\SaveHandler\SaveHandlerInterface $saveHandler
  138. * @return bool
  139. */
  140. protected static function registerSaveHandler(SaveHandlerInterface $saveHandler)
  141. {
  142. return session_set_save_handler(
  143. array($saveHandler, 'open'),
  144. array($saveHandler, 'close'),
  145. array($saveHandler, 'read'),
  146. array($saveHandler, 'write'),
  147. array($saveHandler, 'destroy'),
  148. array($saveHandler, 'gc')
  149. );
  150. }
  151. }