db = clsDatabase::getInstance('test.login'); // Deze classe moet sessies afhandlen, dus geef dat aan session_set_save_handler(array($this,'sess_open'),array($this,'sess_close'),array($this,'sess_read'), array($this,'sess_write'),array($this,'sess_destroy'),array($this,'sess_gc')); session_start(); $this->session_started = true; } public function __destruct() { } public static function getInstance() { /* pre: - post: Geeft instantie van huidig object, altijd maar 1 instantie te gelijk ( SINGLETON ) */ static $instance; if(!isset($instance)){ $object= __CLASS__; $instance = new $object; } return $instance; } public function sess_open ( $save_path, $session_name ) { // Bepaal de max leeftijd van een sessie $this->lifetime = ini_get("session.gc_maxlifetime"); // Als iemand al een sessie had en deze is nog niet verlopen, geef hem dan opnieuw diezelfde sessie $rs = $this->db->query( "SELECT session_id FROM sessions WHERE ip = '" . $_SERVER['REMOTE_ADDR'] . "'"); if ( $rs->CountRows() ) { $rs_arr = $rs->fetchRow(); session_id($rs_arr['session_id']); } return true; } public function sess_close () { $this->sess_gc($this->lifetime); return true; } public function sess_read( $id ) { $rs = $this->db->query( "SELECT data FROM sessions WHERE session_id = '" . $this->db->quote_smart($id) . "'"); if ( $rs->CountRows() ) { $rs_arr = $rs->fetchRow(); return $rs_arr['data']; } else { $rs = $this->db->query("INSERT INTO sessions VALUES ( '', NOW(), '" .$this->db->quote_smart($id) ."', '{$_SERVER['REMOTE_ADDR']}',0)"); return false; } } public function sess_write ( $id, $sess_data ) { // Als iemand al ingelogd is haal dan het id uit de data, nodig voor users_online bijvoorbeeld if ( preg_match ( '/id\|s:(\d+?):\"(\d+?)\"/i',$sess_data, $matches) ) { $user = $matches[2]; } else { $user = 0; } $rs = $this->db->query("INSERT INTO sessions VALUES ( '$sess_data', NOW(), '" . $this->db->quote_smart($id) . "', '{$_SERVER['REMOTE_ADDR']}', '$user') ON DUPLICATE KEY UPDATE data = '" .$this->db->quote_smart($sess_data) . "', user = '$user'"); return true; } public function sess_destroy ( $id ) { $this->db->query("DELETE FROM sessions WHERE session_id = '" .$this->db->quote_smart($id) ."'"); return true; } public function sess_gc ( $maxlifetime ) { // Vernietig alle sessies die verlopen zijn $rs = $this->db->query("DELETE FROM sessions WHERE time + $maxlifetime < NOW()"); return $rs->CountAffectedRows(); } } ?>