欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php把session写入数据库的类

程序员文章站 2022-05-21 13:32:18
...
  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` $sql = sprintf($sql,
  72. mysqli_real_escape_string($this->link, $this->config['table']),
  73. time());
  74. $result = mysqli_query($this->link, $sql);

  75. return mysqli_affected_rows($this->link);
  76. }
  77. }
  78. class session {
  79. public static $collection = null;
  80. public static function open($clean = false, $token = false) {

  81. if($clean) ob_end_clean();
  82. if($token) session_id($token);
  83. session_start();
  84. self::$collection = $_SESSION;
  85. }
  86. public static function id() {

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

  96. return isset($_SESSION[$name]) ? $_SESSION[$name] : null;
  97. }
  98. public static function set($name, $value) {

  99. $_SESSION[$name] = $value;
  100. return true;
  101. }
  102. public static function delete($name) {

  103. if(!isset($_SESSION[$name])) return null;
  104. unset($_SESSION[$name]);
  105. return true;
  106. }
  107. public static function destroy() {

  108. session_destroy();
  109. }
  110. }

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