session.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. # ***** BEGIN LICENSE BLOCK *****
  3. # This file is part of DotClear.
  4. # Copyright (c) 2004 Olivier Meunier and contributors. All rights
  5. # reserved.
  6. #
  7. # DotClear is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # DotClear is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with DotClear; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. #
  21. # ***** END LICENSE BLOCK *****
  22. if(function_exists('ini_set'))
  23. {
  24. @ini_set('session.use_cookies','1');
  25. @ini_set('session.use_only_cookies','1');
  26. @ini_set('url_rewriter.tags','');
  27. @ini_set('session.use_trans_sid','0');
  28. }
  29. // NE PAS DECOMMENTER
  30. //@ini_set('session.use_only_cookies','0');
  31. $con = new connection(DB_USER,DB_PASS,DB_HOST,DB_DBASE);
  32. $dc_session = new dbSession($con,DB_PREFIX.'session');
  33. session_set_save_handler(
  34. array(&$dc_session, '_open'),
  35. array(&$dc_session, '_close'),
  36. array(&$dc_session, '_read'),
  37. array(&$dc_session, '_write'),
  38. array(&$dc_session, '_destroy'),
  39. array(&$dc_session, '_gc')
  40. );
  41. session_name(DC_SESSION_NAME);
  42. session_start();
  43. class dbSession
  44. {
  45. var $con;
  46. var $table;
  47. var $sess_ttl = '-120 minutes';
  48. function dbSession(&$con,$table)
  49. {
  50. $this->con = $con;
  51. $this->table = $table;
  52. }
  53. function _open($path,$name)
  54. {
  55. return true;
  56. }
  57. function _close()
  58. {
  59. $this->_gc();
  60. return true;
  61. }
  62. function _read($ses_id)
  63. {
  64. $strReq = 'SELECT * FROM '.$this->table.' '.
  65. 'WHERE ses_id = \''.$ses_id.'\' ';
  66. $rs = $this->con->select($strReq);
  67. if ($rs->isEmpty()) {
  68. return '';
  69. } else {
  70. return $rs->f('ses_value');
  71. }
  72. }
  73. function _write($ses_id, $data)
  74. {
  75. $strReq = 'UPDATE '.$this->table.' SET '.
  76. 'ses_time = \''.time().'\', '.
  77. 'ses_value = \''.$data.'\' '.
  78. 'WHERE ses_id = \''.$ses_id.'\' ';
  79. if ($this->con->execute($strReq) === false) {
  80. return false;
  81. }
  82. if ($this->con->rowCount() > 0) {
  83. return true;
  84. }
  85. $strReq = 'INSERT INTO '.$this->table.' '.
  86. ' (ses_id, ses_time, ses_start, ses_value) VALUES ('.
  87. '\''.$ses_id.'\','.
  88. '\''.time().'\','.
  89. '\''.time().'\','.
  90. '\''.$data.'\') ';
  91. if ($this->con->execute($strReq) === false) {
  92. return false;
  93. }
  94. return true;
  95. }
  96. function _destroy($ses_id)
  97. {
  98. $strReq = 'DELETE FROM '.$this->table.' '.
  99. 'WHERE ses_id = \''.$ses_id.'\' ';
  100. if ($this->con->execute($strReq) === false) {
  101. return false;
  102. } else {
  103. $this->_optimize();
  104. return true;
  105. }
  106. }
  107. function _gc()
  108. {
  109. $ses_life = strtotime($this->sess_ttl);
  110. $strReq = 'DELETE FROM '.$this->table.' '.
  111. 'WHERE ses_time < '.$ses_life.' ';
  112. if ($this->con->execute($strReq) === false) {
  113. return false;
  114. }
  115. if ($this->con->rowCount() > 0) {
  116. $this->_optimize();
  117. }
  118. return true;
  119. }
  120. function _optimize()
  121. {
  122. $strReq = 'OPTIMIZE TABLE '.$this->table.' ';
  123. if ($this->con->execute($strReq) === false) {
  124. return false;
  125. } else {
  126. return true;
  127. }
  128. }
  129. }
  130. ?>