php session 写入数据库

这篇文章主要介绍了php session 写入数据库的相关资料,需要的朋友可以参考下,本文实例介绍了php session 写入数据库的方法,分享给大家供大家参考,具体内容如下

  1. <?php
  2. #
  3. # codeMaker Alpha 0.1.1 ( haowei.me )
  4. # This framework comply with the GPL license agreement
  5. #
  6. class session_handler {
  7. protected $maxlifetime = null;
  8. protected $dbHandle = null;
  9. public $config = null;
  10. public static function init($args) {
  11. return new self($args);
  12. }
  13. public function __construct($args) {
  14. $this->config = $args;
  15. $this->maxlifetime = get_cfg_var("session.gc_maxlifetime");
  16. session_set_save_handler(
  17. array($this, "open"),
  18. array($this, "close"),
  19. array($this, "read"),
  20. array($this, "write"),
  21. array($this, "destroy"),
  22. array($this, "gc"));
  23. }
  24. public function open() {
  25. $this->link = mysqli_connect(
  26. $this->config['host'],
  27. $this->config['user'],
  28. $this->config['password'],
  29. $this->config['database']);
  30. mysqli_set_charset($this->link, "utf8");
  31. $sql = 'CREATE TABLE IF NOT EXISTS `%s` (
  32. `session_id` varchar(255) NOT NULL,
  33. `session_data` text,
  34. `session_expires` char(10) NOT NULL,
  35. PRIMARY KEY (`session_id`)
  36. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;';
  37. $sql = sprintf($sql, $this->config['table']);
  38. mysqli_query($this->link, $sql);
  39. return true;
  40. }
  41. public function close() {
  42. return true;
  43. }
  44. public function read($session_id) {
  45. if(emptyempty($session_id)) return null;
  46. $sql = 'SELECT `session_data` AS `data` FROM `%s` WHERE `session_id` = "%s" AND `session_expires` > "%u"';
  47. $sql = sprintf($sql,
  48. mysqli_real_escape_string($this->link, $this->config['table']),
  49. mysqli_real_escape_string($this->link, $session_id),
  50. time());
  51. $result = mysqli_query($this->link, $sql);
  52. $row = mysqli_fetch_assoc($result);
  53. return $row['data'];
  54. }
  55. public function write($session_id, $session_data) {
  56. if(emptyempty($session_id)) return null;
  57. $newExpires = time() + $this->maxlifetime;
  58. $sql = 'REPLACE INTO `%s` SET `session_id` = "%s", `session_data` = "%s", `session_expires` = "%u"';
  59. $sql = sprintf($sql,
  60. mysqli_real_escape_string($this->link, $this->config['table']),
  61. mysqli_real_escape_string($this->link, $session_id),
  62. mysqli_real_escape_string($this->link, $session_data),
  63. $newExpires);
  64. $result = mysqli_query($this->link, $sql);
  65. return mysqli_affected_rows($this->link);
  66. }
  67. public function destroy($session_id) {
  68. $sql = 'DELETE FROM `%s` WHERE `session_id` = "%s"';
  69. $sql = sprintf($sql,
  70. mysqli_real_escape_string($this->link, $this->config['table']),
  71. mysqli_real_escape_string($this->link, $session_id));
  72. $result = mysqli_query($this->link, $sql);
  73. return mysqli_affected_rows($this->link);
  74. }
  75. public function gc() {
  76. $sql = 'DELETE FROM `%s` WHERE `session_expires` < "%u"';
  77. $sql = sprintf($sql,
  78. mysqli_real_escape_string($this->link, $this->config['table']),
  79. time());
  80. $result = mysqli_query($this->link, $sql);
  81. return mysqli_affected_rows($this->link);
  82. }
  83. }
  84. class session {
  85. public static $collection = null;
  86. public static function open($clean = false, $token = false) {
  87. if($clean) ob_end_clean();
  88. if($token) session_id($token);
  89. session_start();
  90. self::$collection = $_SESSION;
  91. }
  92. public static function id() {
  93. $num_args = func_num_args();
  94. if($num_args) {
  95. $args = func_get_arg(0);
  96. return session_id($args);
  97. }else{
  98. return session_id();
  99. }
  100. }
  101. public static function get($name) {
  102. return isset($_SESSION[$name]) ? $_SESSION[$name] : null;
  103. }
  104. public static function set($name, $value) {
  105. $_SESSION[$name] = $value;
  106. return true;
  107. }
  108. public static function delete($name) {
  109. if(!isset($_SESSION[$name])) return null;
  110. unset($_SESSION[$name]);
  111. return true;
  112. }
  113. public static function destroy() {
  114. session_destroy();
  115. }
  116. }
  117. $config = array(
  118. "host"=> "127.0.0.1",
  119. "user"=> "root",
  120. "password"=> "123456",
  121. "database"=> "test",
  122. "charset"=> "utf8",
  123. "table"=> "user_session");
  124. session_handler::init($config);
  125. session::open();
  126. session::set("profile", array("id"=> 1, "user"=> "haowei", "vip-level"=> 6));

以上就是本文的全部内容,希望对大家的学习有所帮助。