WeSession.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. namespace app\common\helpers;
  3. class WeSession implements \SessionHandlerInterface{
  4. public static $uniacid;
  5. public static $openid;
  6. public static $expire;
  7. public static function start($uniacid, $openid, $expire = 7200) {
  8. WeSession::$uniacid = $uniacid;
  9. WeSession::$openid = $openid;
  10. WeSession::$expire = $expire;
  11. $cache_setting = $GLOBALS['_W']['config']['setting'];
  12. if (extension_loaded('memcache') && !empty($cache_setting['memcache']['server']) && !empty($cache_setting['memcache']['session'])) {
  13. self::setHandler('memcache');
  14. } elseif (extension_loaded('redis') && !empty($cache_setting['redis']['server']) && !empty($cache_setting['redis']['session'])) {
  15. self::setHandler('redis');
  16. } else {
  17. self::setHandler('mysql');
  18. }
  19. register_shutdown_function('session_write_close');
  20. session_start();
  21. }
  22. public static function setHandler($type = 'mysql') {
  23. $classname = "app\common\helpers\WeSession{$type}";
  24. if (class_exists($classname)) {
  25. $sess = new $classname;
  26. }
  27. if (version_compare(PHP_VERSION, '5.5') >= 0) {
  28. session_set_save_handler($sess, true);
  29. } else {
  30. session_set_save_handler(
  31. array(&$sess, 'open'),
  32. array(&$sess, 'close'),
  33. array(&$sess, 'read'),
  34. array(&$sess, 'write'),
  35. array(&$sess, 'destroy'),
  36. array(&$sess, 'gc')
  37. );
  38. }
  39. return true;
  40. }
  41. public function open($save_path, $session_name) {
  42. return true;
  43. }
  44. public function close() {
  45. return true;
  46. }
  47. public function read($sessionid) {
  48. return '';
  49. }
  50. public function write($sessionid, $data) {
  51. return true;
  52. }
  53. public function destroy($sessionid) {
  54. return true;
  55. }
  56. public function gc($expire) {
  57. return true;
  58. }
  59. }
  60. class WeSessionMemcache extends WeSession {
  61. protected $session_name;
  62. protected function key($sessionid) {
  63. return $this->session_name . ':' . $sessionid;
  64. }
  65. public function open($save_path, $session_name) {
  66. $this->session_name = $session_name;
  67. if (cache_type() != 'memcache') {
  68. trigger_error('Memcache 扩展不可用或是服务未开启,请将 \$config[\'setting\'][\'memcache\'][\'session\'] 设置为0 ');
  69. return false;
  70. }
  71. return true;
  72. }
  73. public function read($sessionid) {
  74. $row = cache_read($this->key($sessionid));
  75. if ($row['expiretime'] < TIMESTAMP) {
  76. return '';
  77. }
  78. if(is_array($row) && !empty($row['data'])) {
  79. return $row['data'];
  80. }
  81. return '';
  82. }
  83. public function write($sessionid, $data) {
  84. if (empty($data) || (!empty($data) && empty($this->chk_member_id_session($data)))) {
  85. $read_data = $this->read($sessionid);
  86. if (!empty($member_data = $this->chk_member_id_session($read_data))) {
  87. $data .= $member_data;
  88. }
  89. }
  90. $row = array();
  91. $row['data'] = $data;
  92. $row['expiretime'] = TIMESTAMP + WeSession::$expire;
  93. return cache_write($this->key($sessionid), $row);
  94. }
  95. public function destroy($sessionid) {
  96. return cache_write($this->key($sessionid), '');
  97. }
  98. public function chk_member_id_session($read_data)
  99. {
  100. $member_data = '';
  101. if (!empty($read_data)) {
  102. preg_match_all('/yunzshop_([\w]+[^|]*|)/', $read_data, $name_matches);
  103. preg_match_all('/(a:[\w]+[^}]*})/', $read_data, $value_matches);
  104. if (!empty($name_matches)) {
  105. foreach ($name_matches[0] as $key => $val) {
  106. if ($val == 'yunzshop_member_id') {
  107. $member_data = $val . '|' . $value_matches[0][$key];
  108. }
  109. }
  110. }
  111. }
  112. return $member_data;
  113. }
  114. }
  115. class WeSessionRedis extends WeSessionMemcache {
  116. public function __construct()
  117. {
  118. }
  119. public function open($save_path, $session_name) {
  120. $this->session_name = $session_name;
  121. if (cache_type() != 'redis') {
  122. trigger_error('Redis 扩展不可用或是服务未开启,请将 \$config[\'setting\'][\'redis\'][\'session\'] 设置为0 ');
  123. return false;
  124. }
  125. return true;
  126. }
  127. }
  128. class WeSessionMysql extends WeSession {
  129. public function open($save_path, $session_name) {
  130. return true;
  131. }
  132. public function read($sessionid) {
  133. $sql = 'SELECT * FROM ' . tablename('core_sessions') . ' WHERE `sid`=:sessid AND `expiretime`>:time';
  134. $params = array();
  135. $params[':sessid'] = $sessionid;
  136. $params[':time'] = TIMESTAMP;
  137. $row = pdo_fetch($sql, $params);
  138. if(is_array($row) && !empty($row['data'])) {
  139. return $row['data'];
  140. }
  141. return '';
  142. }
  143. public function write($sessionid, $data) {
  144. if (empty($data) || (!empty($data) && empty($this->chk_member_id_session($data)))) {
  145. $read_data = $this->read($sessionid);
  146. if (!empty($member_data = $this->chk_member_id_session($read_data))) {
  147. $data .= $member_data;
  148. }
  149. }
  150. $row = array();
  151. $row['sid'] = $sessionid;
  152. $row['uniacid'] = WeSession::$uniacid;
  153. $row['openid'] = WeSession::$openid;
  154. $row['data'] = $data;
  155. $row['expiretime'] = TIMESTAMP + WeSession::$expire;
  156. return pdo_insert('core_sessions', $row, true) >= 1;
  157. }
  158. public function destroy($sessionid) {
  159. $row = array();
  160. $row['sid'] = $sessionid;
  161. return pdo_delete('core_sessions', $row) == 1;
  162. }
  163. public function gc($expire) {
  164. $sql = 'DELETE FROM ' . tablename('core_sessions') . ' WHERE `expiretime`<:expire';
  165. return pdo_query($sql, array(':expire' => TIMESTAMP)) == 1;
  166. }
  167. private function chk_member_id_session($read_data)
  168. {
  169. $member_data = '';
  170. if (!empty($read_data)) {
  171. preg_match_all('/yunzshop_([\w]+[^|]*|)/', $read_data, $name_matches);
  172. preg_match_all('/(a:[\w]+[^}]*})/', $read_data, $value_matches);
  173. if (!empty($name_matches)) {
  174. foreach ($name_matches[0] as $key => $val) {
  175. if ($val == 'yunzshop_member_id') {
  176. $member_data = $val . '|' . $value_matches[0][$key];
  177. }
  178. }
  179. }
  180. }
  181. return $member_data;
  182. }
  183. }