PHP将session信息存储到数据库的类实例

这篇文章主要介绍了PHP将session信息存储到数据库的类,实例分析了php封装的session信息存储到数据库的类,具有一定参考借鉴价值,需要的朋友可以参考下。

本文实例讲述了PHP将session信息存储到数据库的类,分享给大家供大家参考,具体分析如下:

SessionHandlerInterface接口是PHP内置的接口,直接实现就行了

具体可以看php手册关于session_set_save_handler函数的解释!

PHP代码如下:

  1. /**
  2. * session信息存储到数据库的类
  3. * 表结构:
  4. * CREATE TABLE IF NOT EXISTS `sessioninfo` (
  5. * `sid` varchar(255) NOT NULL,
  6. * `value` text NOT NULL,
  7. * `expiration` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  8. * PRIMARY KEY (`sid`)
  9. * ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  10. */
  11. class MySessionHandler implements SessionHandlerInterface {
  12. /**
  13. * @access private
  14. * @var object 数据库连接
  15. */
  16. private $_dbLink;
  17. /**
  18. * @access private
  19. * @var string 保存session的表名
  20. */
  21. Private $_sessionTable;
  22. /**
  23. * @access private
  24. * @var string session名
  25. */
  26. private $_sessionName;
  27. /**
  28. * @const 过期时间
  29. */
  30. const SESSION_EXPIRE = 10;
  31. public function __construct($dbLink, $sessionTable) {
  32. if(!is_object($dbLink)) {
  33. return false;
  34. }
  35. $this->_dbLink = $dbLink;
  36. $this->_sessionTable = $sessionTable;
  37. }
  38. /**
  39. * 打开
  40. * @access public
  41. * @param string $session_save_path 保存session的路径
  42. * @param string $session_name session名
  43. * @return integer
  44. */
  45. public function open($session_save_path, $session_name) {
  46. $this->_sessionName = $session_name;
  47. return 0;
  48. }
  49. /**
  50. * 关闭
  51. * @access public
  52. * @return integer
  53. */
  54. public function close() {
  55. return 0;
  56. }
  57. /**
  58. * 关闭session
  59. * @access public
  60. * @param string $session_id session ID
  61. * @return string
  62. */
  63. public function read($session_id) {
  64. $query = "SELECT value FROM {$this->_sessionTable} WHERE sid = {$session_id} AND UNIX_TIMESTAMP(expiration) + " . self::SESSION_EXPIRE . " > UNIX_TIMESTAMP(NOW())";
  65. $result = $this->_dbLink->query($query);
  66. if(!isset($value) || emptyempty($value)) {
  67. $value = "";
  68. return $value;
  69. }
  70. $this->_dbLink->query("UPDATE {$this->_sessionTable} SET expiration = CURRENT_TIMESTAMP() WHERE sid = {$session_id}");
  71. $value = $result->fetch_array();
  72. $result->free();
  73. return $value['value'];
  74. }
  75. /**
  76. * 写入session
  77. * @access public
  78. * @param string $session_id session ID
  79. * @param string $session_data session data
  80. * @return integer
  81. */
  82. public function write($session_id, $session_data) {
  83. $query = "SELECT value FROM {$this->_sessionTable} WHERE sid = '{$session_id}' AND UNIX_TIMESTAMP(expiration) + " . self::SESSION_EXPIRE . " > UNIX_TIMESTAMP(NOW())";
  84. $result = $this->_dbLink->query($query);
  85. $result = $result->fetch_array();
  86. if(!emptyempty($result)) {
  87. $result = $this->_dbLink->query("UPDATE {$this->_sessionTable} SET value = {$session_data} WHERE sid = {$session_id}");
  88. }
  89. else{
  90. $result = $this->_dbLink->query("INSERT INTO {$this->_sessionTable} (sid, value) VALUES ('{$session_id}', '{$session_data}')");
  91. }
  92. if($result){
  93. return 0;
  94. }
  95. else{
  96. return 1;
  97. }
  98. }
  99. /**
  100. * 销魂session
  101. * @access public
  102. * @param string $session_id session ID
  103. * @return integer
  104. */
  105. public function destroy($session_id) {
  106. $result = $this->_dbLink->query("DELETE FROM {$this->_sessionTable} WHERE sid = '{$session_id}'");
  107. if($result){
  108. return 0;
  109. }
  110. else{
  111. return 1;
  112. }
  113. }
  114. /**
  115. * 垃圾回收
  116. * @access public
  117. * @param string $maxlifetime session 最长生存时间
  118. * @return integer
  119. */
  120. public function gc($maxlifetime) {
  121. $result = $this->_dbLink->query("DELETE FROM {$this->_sessionTable} WHERE UNIX_TIMESTAMP(expiration) < UNIX_TIMESTAMP(NOW()) - " . self::SESSION_EXPIRE);
  122. if($result){
  123. return 0;
  124. }
  125. else{
  126. return 1;
  127. }
  128. }
  129. }
  130. $dbLink = new mysqli("localhost", "root", "root", "test");
  131. $sessionTable = "sessioninfo";
  132. $handler = new MySessionHandler($dbLink, $sessionTable);
  133. session_set_save_handler($handler);
  134. session_start();
  135. $_SESSION['name'] = "test";
  136. echo $_SESSION["name"];
  137. //session_destroy();