php实现将Session写入数据库

这篇文章主要介绍了php实现将Session写入数据库的相关资料,需要的朋友可以参考下,使用session_set_save_handler()函数,将Session的内容写入数据库。

  1. <?php
  2. /*
  3. *@author Fahy
  4. *数据库为mysql,
  5. *数据库名为session,表名为session,
  6. *表中字段包括PHPSESSID,update_time,client_ip,data
  7. */
  8. class Session{
  9. private static $handler = null;
  10. private static $ip = null;
  11. private static $lifetime = null;
  12. private static $time = null;
  13. //配置静态变量
  14. private static function init($handler){
  15. self::$handler = $handler; //获取数据库资源
  16. self::$ip = !emptyempty($_SERVER["REMOTE_ADDR"])? $_SERVER["REMOTE_ADDR"]:'unkonw'; //获取客户端ip
  17. self::$lifetime = ini_get('session.gc_maxlifetime'); //获取session生命周期
  18. self::$time = time(); //获取当前时间
  19. }
  20. //调用session_set_save_handler()函数并开启session
  21. static function start($pdo){
  22. self::init($pdo);
  23. session_set_save_handler(
  24. array(__CLASS__,'open'),
  25. array(__CLASS__,'close'),
  26. array(__CLASS__,'read'),
  27. array(__CLASS__,'write'),
  28. array(__CLASS__,'destroy'),
  29. array(__CLASS__,'gc')
  30. );
  31. session_start();
  32. }
  33. public static function open($path,$name){
  34. return true;
  35. }
  36. public static function close(){
  37. return true;
  38. }
  39. //查询数据库中的数据
  40. public static function read($PHPSESSID){
  41. $sql = "select PHPSESSID,update_time,client_ip,data from session where PHPSESS;
  42. $stmt = self::$handler->prepare($sql);
  43. $stmt->execute(array($PHPSESSID));
  44. if(!$result = $stmt->fetch(PDO::FETCH_ASSOC)){
  45. return '';
  46. }
  47. if(self::$ip == $result['client_ip']){
  48. self::destroy($PHPSESSID);
  49. return '';
  50. }
  51. if(($result['update_time']+self::$lifetime)<self::$time){
  52. self::destroy($PHPSESSID);
  53. return '';
  54. }
  55. return $result['data'];
  56. }
  57. /*
  58. *首先查询该session是否存在数据,如果存在,则更新数据,如果不存在,则插入数据
  59. */
  60. //将session写入数据库中,$data传入session中的keys和values数组
  61. public static function write($PHPSESSID,$data){
  62. $sql = "select PHPSESSID,update_time,client_ip,data from session where PHPSESS;
  63. $stmt = self::$handler->prepare($sql);
  64. $stmt->execute(array($PHPSESSID));
  65. if($result=$stmt->fetch(PDO::FETCH_ASSOC)){
  66. if($result['data'] != $data || self::$time > ($result['update_time']+30)){
  67. $sql = "update session set update_time=?,data=? where PHPSESSID = ?";
  68. $stmt = self::$handler->prepare($sql);
  69. $stmt->execute(array($self::$time,$data,$PHPSESSID));
  70. }
  71. }else{
  72. if(!emptyempty($data)){
  73. try{
  74. $sql = "insert into session(PHPSESSID,update_time,client_ip,data) values(?,?,?,?)";
  75. }catch(PDOException $e){
  76. echo $e->getMessage();
  77. }
  78. $sth = self::$handler->prepare($sql);
  79. $sth->execute(array($PHPSESSID,self::$time,self::$ip,$data));
  80. }
  81. }
  82. return true;
  83. }
  84. public static function destroy($PHPSESSID){
  85. $sql = "delete from session where PHPSESSID = ?";
  86. $stmt = self::$handler->prepare($sql);
  87. $stmt->execute(array($PHPSESSID));
  88. return true;
  89. }
  90. public static function gc($lifetime){
  91. $sql = "delete from session where update_time<?";
  92. $stmt = self::$handler->prepare($sql);
  93. $stmt->execute(array(self::$time-$lifetime));
  94. return true;
  95. }
  96. }
  97. //使用PDO连接数据库
  98. try{
  99. $pdo = new PDO("mysql:host=localhost;dbname=session","root","hwj193");
  100. }catch(PDOException $e){
  101. echo $e->getMessage();
  102. }
  103. //传递数据库资源
  104. Session::start($pdo);

以上所述就是本文的全部内容了,希望大家能够喜欢。