session_handler.class.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This class allows to manage the session. Session is stored in
  5. * the database
  6. *
  7. * @package chamilo.library
  8. */
  9. class session_handler {
  10. public $connexion;
  11. public $idConnexion;
  12. public $lifetime;
  13. public $sessionName;
  14. public function __construct () {
  15. global $_configuration;
  16. $this->lifetime=60; // 60 minutes
  17. $this->connexion=array('server' => $_configuration['db_host'],'login' => $_configuration['db_user'],'password' => $_configuration['db_password'],'base' => $_configuration['main_database']);
  18. $this->idConnexion=false;
  19. }
  20. public function sqlConnect () {
  21. if(!$this->idConnexion)
  22. {
  23. $this->idConnexion=@mysql_connect($this->connexion['server'],$this->connexion['login'],$this->connexion['password'],true);
  24. // The system has not been designed to use special SQL modes that were introduced since MySQL 5
  25. @mysql_query("set session sql_mode='';", $this->idConnexion);
  26. }
  27. return $this->idConnexion?true:false;
  28. }
  29. public function sqlClose() {
  30. if($this->idConnexion)
  31. {
  32. mysql_close($this->idConnexion);
  33. $this->idConnexion=false;
  34. return true;
  35. }
  36. return false;
  37. }
  38. public function sqlQuery ($query,$die_on_error=true) {
  39. $result=mysql_query($query,$this->idConnexion);
  40. if($die_on_error && !$result)
  41. {
  42. $this->sqlClose();
  43. return;
  44. //exit();
  45. }
  46. return $result;
  47. }
  48. public function open ($path,$name) {
  49. $this->sessionName=$name;
  50. return true;
  51. }
  52. public function close () {
  53. return $this->garbage(0)?true:false;
  54. }
  55. public function read ($sess_id) {
  56. if($this->sqlConnect())
  57. {
  58. $result=$this->sqlQuery("SELECT session_value FROM ".$this->connexion['base'].".php_session WHERE session_id='$sess_id'");
  59. if($row=mysql_fetch_assoc($result))
  60. {
  61. return $row['session_value'];
  62. }
  63. }
  64. return '';
  65. }
  66. public function write ($sess_id,$sess_value) {
  67. $time=time();
  68. if($this->sqlConnect())
  69. {
  70. $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);
  71. if(!$result)
  72. {
  73. $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'");
  74. }
  75. return true;
  76. }
  77. return false;
  78. }
  79. public function destroy ($sess_id) {
  80. if($this->sqlConnect())
  81. {
  82. $this->sqlQuery("DELETE FROM ".$this->connexion['base'].".php_session WHERE session_id='$sess_id'");
  83. return true;
  84. }
  85. return false;
  86. }
  87. public function garbage ($lifetime) {
  88. if($this->sqlConnect())
  89. {
  90. $result=$this->sqlQuery("SELECT COUNT(session_id) FROM ".$this->connexion['base'].".php_session");
  91. list($nbr_results)=Database::fetch_row($result);
  92. if($nbr_results > 5000)
  93. {
  94. $this->sqlQuery("DELETE FROM ".$this->connexion['base'].".php_session WHERE session_time<'".strtotime('-'.$this->lifetime.' minutes')."'");
  95. }
  96. $this->sqlClose();
  97. return true;
  98. }
  99. return false;
  100. }
  101. };
  102. ?>