session_handler.class.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php // $Id: session_handler.class.php 10684 2007-01-11 22:39:42Z yannoo $
  2. /*
  3. ===============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2007 Dokeos S.A.
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. Copyright (c) Hugues Peeters
  9. Copyright (c) Christophe Gesche
  10. Copyright (c) Roan Embrechts
  11. Copyright (c) Patrick Cool
  12. Copyright (c) Olivier Brouckaert
  13. Copyright (c) Toon Van Hoecke
  14. Copyright (c) Denes Nagy
  15. For a full list of contributors, see "credits.txt".
  16. The full license can be read in "license.txt".
  17. This program is free software; you can redistribute it and/or
  18. modify it under the terms of the GNU General Public License
  19. as published by the Free Software Foundation; either version 2
  20. of the License, or (at your option) any later version.
  21. See the GNU General Public License for more details.
  22. Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
  23. ===============================================================================
  24. */
  25. /**
  26. ==============================================================================
  27. * This class allows to manage the session. Session is stored in
  28. * the database
  29. *
  30. * @package dokeos.library
  31. ==============================================================================
  32. */
  33. class session_handler
  34. {
  35. var $connexion;
  36. var $idConnexion;
  37. var $lifetime;
  38. var $sessionName;
  39. function session_handler()
  40. {
  41. global $_configuration;
  42. $this->lifetime=60; // 60 minutes
  43. $this->connexion=array('server' => $_configuration['db_host'],'login' => $_configuration['db_user'],'password' => $_configuration['db_password'],'base' => $_configuration['main_database']);
  44. $this->idConnexion=false;
  45. }
  46. function sqlConnect()
  47. {
  48. if(!$this->idConnexion)
  49. {
  50. $this->idConnexion=@mysql_connect($this->connexion['server'],$this->connexion['login'],$this->connexion['password'],true);
  51. }
  52. return $this->idConnexion?true:false;
  53. }
  54. function sqlClose()
  55. {
  56. if($this->idConnexion)
  57. {
  58. mysql_close($this->idConnexion);
  59. $this->idConnexion=false;
  60. return true;
  61. }
  62. return false;
  63. }
  64. function sqlQuery($query,$die_on_error=true)
  65. {
  66. $result=mysql_query($query,$this->idConnexion);
  67. if($die_on_error && !$result)
  68. {
  69. $this->sqlClose();
  70. exit();
  71. }
  72. return $result;
  73. }
  74. function open($path,$name)
  75. {
  76. $this->sessionName=$name;
  77. return true;
  78. }
  79. function close()
  80. {
  81. return $this->garbage(0)?true:false;
  82. }
  83. function read($sess_id)
  84. {
  85. if($this->sqlConnect())
  86. {
  87. $result=$this->sqlQuery("SELECT session_value FROM ".$this->connexion['base'].".php_session WHERE session_id='$sess_id'");
  88. if($row=mysql_fetch_assoc($result))
  89. {
  90. return $row['session_value'];
  91. }
  92. }
  93. return '';
  94. }
  95. function write($sess_id,$sess_value)
  96. {
  97. $time=time();
  98. if($this->sqlConnect())
  99. {
  100. $result=$this->sqlQuery("INSERT INTO ".$this->connexion['base'].".php_session(session_id,session_name,session_time,session_start,session_value) VALUES('$sess_id','".$this->sessionName."','$time','$time','".addslashes($sess_value)."')",false);
  101. if(!$result)
  102. {
  103. $this->sqlQuery("UPDATE ".$this->connexion['base'].".php_session SET session_name='".$this->sessionName."',session_time='$time',session_value='".addslashes($sess_value)."' WHERE session_id='$sess_id'");
  104. }
  105. return true;
  106. }
  107. return false;
  108. }
  109. function destroy($sess_id)
  110. {
  111. if($this->sqlConnect())
  112. {
  113. $this->sqlQuery("DELETE FROM ".$this->connexion['base'].".php_session WHERE session_id='$sess_id'");
  114. return true;
  115. }
  116. return false;
  117. }
  118. function garbage($lifetime)
  119. {
  120. if($this->sqlConnect())
  121. {
  122. $result=$this->sqlQuery("SELECT COUNT(session_id) FROM ".$this->connexion['base'].".php_session");
  123. list($nbr_results)=mysql_fetch_row($result);
  124. if($nbr_results > 5000)
  125. {
  126. $this->sqlQuery("DELETE FROM ".$this->connexion['base'].".php_session WHERE session_time<'".strtotime('-'.$this->lifetime.' minutes')."'");
  127. }
  128. $this->sqlClose();
  129. return true;
  130. }
  131. return false;
  132. }
  133. };
  134. ?>