Home >Backend Development >PHP Tutorial >PHP class that writes session to database

PHP class that writes session to database

WBOY
WBOYOriginal
2016-07-25 08:54:11722browse
  1. class session_handler {

  2. protected $maxlifetime = null;

  3. protected $dbHandle = null;
  4. public $config = null;

  5. public static function init($args) {

  6. return new self($args);
  7. }

  8. public function __construct($args) {

  9. $this->config = $args;

  10. $this->maxlifetime = get_cfg_var("session.gc_maxlifetime");
  11. session_set_save_handler(
  12. array($this, "open"),
  13. array($this, "close"),
  14. array($this, "read"),
  15. array($this, "write"),
  16. array($this, "destroy"),
  17. array($this, "gc"));
  18. }

  19. public function open() {

  20. $this->link = mysqli_connect(
  21. $this->config['host'],
  22. $this->config['user'],
  23. $this->config['password'],
  24. $this->config['database']);
  25. mysqli_set_charset($this->link, "utf8");
  26. $sql = 'CREATE TABLE IF NOT EXISTS `%s` (
  27. `session_id` varchar(255) NOT NULL,
  28. `session_data` text,
  29. `session_expires` char(10) NOT NULL,
  30. PRIMARY KEY (`session_id`)
  31. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;';
  32. $sql = sprintf($sql, $this->config['table']);
  33. mysqli_query($this->link, $sql);
  34. return true;
  35. }

  36. public function close() {

  37. return true;
  38. }

  39. public function read($session_id) {

  40. if(empty($session_id)) return null;

  41. $sql = 'SELECT `session_data` AS `data` FROM `%s` WHERE `session_id` = "%s" AND `session_expires` > "%u"';

  42. $sql = sprintf($sql,
  43. mysqli_real_escape_string($this->link, $this->config['table']),
  44. mysqli_real_escape_string($this->link, $session_id),
  45. time());

  46. $result = mysqli_query($this->link, $sql);

  47. $row = mysqli_fetch_assoc($result);
  48. return $row['data'];
  49. }

  50. public function write($session_id, $session_data) {

  51. if(empty($session_id)) return null;

  52. $newExpires = time() + $this->maxlifetime;

  53. $sql = 'REPLACE INTO `%s` SET `session_id` = "%s", `session_data` = "%s", `session_expires` = "%u"';
  54. $sql = sprintf($sql,
  55. mysqli_real_escape_string($this->link, $this->config['table']),
  56. mysqli_real_escape_string($this->link, $session_id),
  57. mysqli_real_escape_string($this->link, $session_data),
  58. $newExpires);

  59. $result = mysqli_query($this->link, $sql);

  60. return mysqli_affected_rows($this->link);
  61. }

  62. public function destroy($session_id) {

  63. $sql = 'DELETE FROM `%s` WHERE `session_id` = "%s"';
  64. $sql = sprintf($sql,
  65. mysqli_real_escape_string($this->link, $this->config['table']),
  66. mysqli_real_escape_string($this->link, $session_id));
  67. $result = mysqli_query($this->link, $sql);
  68. return mysqli_affected_rows($this->link);
  69. }

  70. public function gc() {

  71. $sql = 'DELETE FROM `%s` WHERE `session_expires` < "%u"';
  72. $sql = sprintf($sql,
  73. mysqli_real_escape_string($this->link, $this->config['table']),
  74. time());

  75. $result = mysqli_query($this->link, $sql);

  76. return mysqli_affected_rows($this->link);
  77. }
  78. }
  79. class session {
  80. public static $collection = null;

  81. public static function open($clean = false, $token = false) {

  82. if($clean) ob_end_clean();
  83. if($token) session_id($token);
  84. session_start();
  85. self::$collection = $_SESSION;
  86. }

  87. public static function id() {

  88. $num_args = func_num_args();
  89. if($num_args) {
  90. $args = func_get_arg(0);
  91. return session_id($args);
  92. }else{
  93. return session_id();
  94. }
  95. }

  96. public static function get($name) {

  97. return isset($_SESSION[$name]) ? $_SESSION[$name] : null;
  98. }

  99. public static function set($name, $value) {

  100. $_SESSION[$name] = $value;
  101. return true;
  102. }

  103. public static function delete($name) {

  104. if(!isset($_SESSION[$name])) return null;
  105. unset($_SESSION[$name]);
  106. return true;
  107. }

  108. public static function destroy() {

  109. session_destroy();
  110. }

  111. }

  112. $config = array(
  113. "host"=> "127.0.0.1",
  114. "user"=> "root",
  115. "password"=> "123456",
  116. "database"=> "test",
  117. "charset"=> "utf8",
  118. "table"=> "user_session");
  119. session_handler::init($config);
  120. session::open();
  121. session::set("profile", array("id"=> 1, "user"=> "haowei", "vip-level"=> 6));

复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn