SESSION存放在数据库用法实例

这篇文章主要介绍了SESSION存放在数据库用法,自定义了一个简单的针对数据操作的session类并给出了使用该类存储到数据库的相关技巧,需要的朋友可以参考下

本文实例讲述了SESSION存放在数据库用法,分享给大家供大家参考,具体如下:

  1. <?php
  2. /*
  3. CREATE TABLE `ws_sessions` (
  4. `session_id` varchar(255) binary NOT NULL default '',
  5. `session_expires` int(10) unsigned NOT NULL default '0',
  6. `session_data` text,
  7. PRIMARY KEY (`session_id`)
  8. ) TYPE=InnoDB;
  9. */
  10. class session {
  11. // session-lifetime
  12. var $lifeTime;
  13. // mysql-handle
  14. var $dbHandle;
  15. function open($savePath, $sessName) {
  16. // get session-lifetime
  17. $this->lifeTime = get_cfg_var("session.gc_maxlifetime");
  18. // open database-connection
  19. $dbHandle = @mysql_connect("localhost","root","");
  20. $dbSel = @mysql_select_db("test",$dbHandle);
  21. // return success
  22. if(!$dbHandle || !$dbSel)
  23. return false;
  24. $this->dbHandle = $dbHandle;
  25. return true;
  26. }
  27. function close() {
  28. $this->gc(ini_get('session.gc_maxlifetime'));
  29. // close database-connection
  30. return @mysql_close($this->dbHandle);
  31. }
  32. function read($sessID) {
  33. // fetch session-data
  34. $res = mysql_query("SELECT session_data AS d FROM ws_sessions
  35. WHERE session_id = '$sessID'
  36. AND session_expires > ".time(),$this->dbHandle);
  37. // return data or an empty string at failure
  38. if($row = mysql_fetch_assoc($res))
  39. return $row['d'];
  40. return "";
  41. }
  42. function write($sessID,$sessData) {
  43. // new session-expire-time
  44. $newExp = time() + $this->lifeTime;
  45. // is a session with this id in the database?
  46. $res = mysql_query("SELECT * FROM ws_sessions
  47. WHERE session_id = '$sessID'",$this->dbHandle);
  48. // if yes,
  49. if(mysql_num_rows($res)) {
  50. // ...update session-data
  51. mysql_query("UPDATE ws_sessions
  52. SET session_expires = '$newExp',
  53. session_data = '$sessData'
  54. WHERE session_id = '$sessID'",$this->dbHandle);
  55. // if something happened, return true
  56. if(mysql_affected_rows($this->dbHandle))
  57. return true;
  58. }
  59. // if no session-data was found,
  60. else {
  61. // create a new row
  62. mysql_query("INSERT INTO ws_sessions (
  63. session_id,
  64. session_expires,
  65. session_data)
  66. VALUES(
  67. '$sessID',
  68. '$newExp',
  69. '$sessData')",$this->dbHandle);
  70. // if row was created, return true
  71. if(mysql_affected_rows($this->dbHandle))
  72. return true;
  73. }
  74. // an unknown error occured
  75. return false;
  76. }
  77. function destroy($sessID) {
  78. // delete session-data
  79. mysql_query("DELETE FROM ws_sessions WHERE session_id = '$sessID'",$this->dbHandle);
  80. // if session was deleted, return true,
  81. if(mysql_affected_rows($this->dbHandle))
  82. return true;
  83. // ...else return false
  84. return false;
  85. }
  86. function gc($sessMaxLifeTime) {
  87. // delete old sessions
  88. mysql_query("DELETE FROM ws_sessions WHERE session_expires < ".time(),$this->dbHandle);
  89. // return affected rows
  90. return mysql_affected_rows($this->dbHandle);
  91. }
  92. }
  93. $session = new session();
  94. session_set_save_handler(array(&$session,"open"),
  95. array(&$session,"close"),
  96. array(&$session,"read"),
  97. array(&$session,"write"),
  98. array(&$session,"destroy"),
  99. array(&$session,"gc"));
  100. session_start();
  101. // etc...
  102. ?>

希望本文所述对大家的php程序设计有所帮助。