php基于session实现数据库交互的类实例

这篇文章主要介绍了php基于session实现数据库交互的类,实例分析了php结合session操作数据库的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下,本文实例讲述了php基于session实现数据库交互的类,分享给大家供大家参考,具体如下:

  1. <?php
  2. /**
  3. * session 数据库存储类
  4. */
  5. class Session {
  6. private static $session_id = 0;
  7. private static $session_data = array();
  8. private static $is_update = FALSE;
  9. private static $is_del = FALSE;
  10. private static $is_gc = FALSE;
  11. private static $dbo = NULL; //数据库连接句柄
  12. private static $gc_max_time = 1440;
  13. private static $table = 'sessions';
  14. private static $pre_key = 'weige';//session 密钥
  15. //捆绑使用哈
  16. private static $gc_rate_de = 100;//代表分母
  17. private static $gc_rate_co = 20;//代表分子
  18. private static $path = '/';//保存路径
  19. private static $domain = null; //域
  20. private static $secure = false;//默认
  21. private static $httponly = false;//默认
  22. /**
  23. * 获取数据库句柄 私有
  24. */
  25. private static function open()
  26. {
  27. if (!self::$dbo)
  28. {
  29. self::$dbo = Db::factory();
  30. }
  31. return TRUE;
  32. }
  33. /**
  34. * 设置
  35. * */
  36. public static function set($key, $val=NULL)
  37. {
  38. self::open();
  39. $data = self::read();
  40. if ($data === FALSE)
  41. {
  42. $data = array();
  43. }
  44. if (!$val && is_array($key))
  45. {
  46. $data = $key;
  47. }
  48. else if ($val && is_string($key))
  49. {
  50. $data[$key] = $val;
  51. }
  52. self::write($data);
  53. self::close();
  54. }
  55. /**
  56. *获取值
  57. *
  58. */
  59. public static function get($key=NULL) {
  60. self::open();
  61. self::$session_data = self::read();
  62. $ret = '';
  63. if (!$key) {
  64. $ret = self::$session_data;
  65. } else if(is_array(self::$session_data) && isset(self::$session_data[$key])) {
  66. $ret = self::$session_data[$key];
  67. }
  68. self::update();
  69. self::close();
  70. return $ret;
  71. }
  72. /**
  73. * 删除或者重置
  74. * */
  75. public static function del($key)
  76. {
  77. if (!self::$is_del)
  78. {
  79. self::open();
  80. $val = self::read();
  81. if (isset($val[$key]))
  82. {
  83. unset($val[$key]);
  84. }
  85. $session_id = self::$session_id;
  86. $session_data = serialize($val);
  87. $session_expire = TIME + self::get_gc_maxtime();
  88. self::$dbo->query("update ".self::$table." set value='$session_data', expiry='$session_expire' where session_");
  89. self::close();
  90. }
  91. self::$is_del = TRUE;
  92. }
  93. /**
  94. * 销毁
  95. *
  96. * */
  97. public static function destroy()
  98. {
  99. $session_id = self::get_session_id();
  100. $_COOKIE['WBSID'] = '';
  101. self::open();
  102. self::$dbo->query("delete from ".self::$table." where session_");
  103. self::close();
  104. }
  105. /**
  106. * 读取 私有
  107. * */
  108. private static function read()
  109. {
  110. $session_id = self::$session_id;
  111. if (!$session_id) {
  112. $session_id = self::get_session_id();
  113. }
  114. if (!$session_id) return array();
  115. $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? md5($_SERVER['HTTP_USER_AGENT']) : '';
  116. $client_ip = Fun::getIp();
  117. $session_expire = TIME - self::get_gc_maxtime();
  118. $rs = self::$dbo->fetchRow("select session_id, value, agent, ip from ".self::$table."
  119. where session_ and expiry>'$session_expire'");
  120. if (!$rs || $rs['agent'] != $user_agent || $rs['ip'] != $client_ip)
  121. {
  122. return FALSE;
  123. }
  124. self::$session_id = $rs['session_id'];
  125. return unserialize($rs['value']);
  126. }
  127. /**
  128. * session 写入 私有
  129. * */
  130. private static function write(array $session_data)
  131. {
  132. $session_id = self::$session_id;
  133. if (!$session_id)
  134. {
  135. $session_id = self::get_session_id();
  136. }
  137. $session_expire = TIME + self::get_gc_maxtime();
  138. $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? md5($_SERVER['HTTP_USER_AGENT']) : '';
  139. $client_ip = Fun::getIp();
  140. $session_data = serialize($session_data);
  141. if (self::$session_id && self::$session_id === $session_id)
  142. {
  143. self::$dbo->query("update ".self::$table." set value='$session_data', expiry='$session_expire', agent='$user_agent', ip='$client_ip' where session_");
  144. }
  145. else
  146. {
  147. self::$session_id = $session_id = self::create_session_id();
  148. self::$dbo->query("insert into ".self::$table."(session_id, value, expiry, agent, ip)
  149. values('$session_id', '$session_data', '$session_expire', '$user_agent', '$client_ip')");
  150. }
  151. return true;
  152. }
  153. /**
  154. * session 更新 私有
  155. * */
  156. private static function update()
  157. {
  158. if (!self::$is_update)
  159. {
  160. $session_id = self::$session_id;
  161. $session_expire = TIME + self::get_gc_maxtime();
  162. self::$dbo->query("update ".self::$table." set expiry='$session_expire' where session_");
  163. }
  164. self::$is_update = TRUE;
  165. }
  166. private static function close()
  167. {
  168. if (!self::$is_gc && mt_rand(1, self::$gc_rate_de)%self::$gc_rate_co == 0)
  169. {
  170. self::gc();
  171. }
  172. self::$is_gc = TRUE;
  173. }
  174. /**
  175. * 过期session 清除 随机触发
  176. * */
  177. private static function gc()
  178. {
  179. $session_expire = TIME - self::get_gc_maxtime();
  180. self::$dbo->query("delete from ".self::$table." where expiry<'$session_expire'");
  181. }
  182. private static function get_session_id()
  183. {
  184. if (isset($_COOKIE['WBSID']) && strlen($_COOKIE['WBSID'])==32)
  185. {
  186. $sid = $_COOKIE['WBSID'];
  187. setcookie('WBSID', $sid, TIME + self::get_gc_maxtime(), self::$path, self::$domain, self::$secure, self::$httponly);
  188. return $sid;
  189. }
  190. return null;
  191. }
  192. private static function create_session_id()
  193. {
  194. $sid = self::get_session_id();
  195. if (!$sid)
  196. {
  197. $sid = Fun::getIp() . TIME . microtime(TRUE) . mt_rand(mt_rand(0, 100), mt_rand(100000, 90000000));
  198. $sid = md5(self::$pre_key . $sid);
  199. setcookie('WBSID', substr($sid, 0, 32), TIME + self::get_gc_maxtime(), self::$path, self::$domain, self::$secure, self::$httponly);
  200. }
  201. return $sid;
  202. }
  203. public static function get_gc_maxtime()
  204. {
  205. return self::$gc_max_time;
  206. }
  207. }

希望本文所述对大家的php程序设计有所帮助。