db = $db; if (isset($_COOKIE[COOKIE_NAME])) { $result = $this->select(SESSIONS_TABLE, '*', 'session_id=\''.$_COOKIE[COOKIE_NAME].'\' AND expiry_time>=NOW()'); if (count($result) > 0) { //update session expiry time in database and in cookie if ($result[0]['IP'] != $_SERVER['REMOTE_ADDR']) { $this->deleteSession(); } else { $this->updateSession(); $uid = $result[0]['user_id']; $result = $this->select(USERS_TABLE, 'user_id, nick, rank', 'user_id=\''.$uid.'\''); $this->userdata = $result[0]; } } else { $this->deleteSession(); } } } public function updateSession() { /*$this->db->query('UPDATE '.SESSIONS_TABLE.' SET expiry_time=(NOW() + INTERVAL 120 MINUTE) WHERE session_id=\''.$_COOKIE[COOKIE_NAME].'\''); setcookie(COOKIE_NAME, $_COOKIE[COOKIE_NAME], $_SERVER['REQUEST_TIME']+7200, COOKIE_PATH, COOKIE_DOMAIN, false, true); */ $newid = $this->generateSessionID(); $this->db->query('UPDATE '.SESSIONS_TABLE.' SET expiry_time=(NOW() + INTERVAL 120 MINUTE), session_id=\''.$newid.'\' WHERE session_id=\''.$_COOKIE[COOKIE_NAME].'\''); setcookie(COOKIE_NAME, $newid, $_SERVER['REQUEST_TIME']+7200, COOKIE_PATH, COOKIE_DOMAIN, false, true); $_COOKIE[COOKIE_NAME] = $newid; } public function deleteSession() { setcookie(COOKIE_NAME, '', $_SERVER['REQUEST_TIME']-3600, COOKIE_PATH, COOKIE_DOMAIN, false, true); $this->db->query('DELETE FROM '.SESSIONS_TABLE.' WHERE session_id=\''.$_COOKIE[COOKIE_NAME].'\''); } private function generateSessionID() { $out = $_SERVER['HTTP_USER_AGENT'].$_SERVER['REQUEST_TIME_FLOAT'].$_SERVER['REMOTE_ADDR'].rand(1, 50000); return md5($out); } public function registerNewSession($user_id) { $newID = $this->generateSessionID(); $query = 'UPDATE '.USERS_INFO_TABLE.' SET lastvisit=NOW() WHERE user_id=\''.$user_id.'\''; $this->db->query($query); $query = 'INSERT INTO '.SESSIONS_TABLE.' (session_id, user_id, IP, expiry_time) VALUES (\''.$newID.'\', \''.$user_id.'\', \''.$_SERVER['REMOTE_ADDR'].'\', (NOW() + INTERVAL 120 MINUTE) );'; $this->db->query($query); setcookie(COOKIE_NAME, $newID, $_SERVER['REQUEST_TIME']+7200, COOKIE_PATH, COOKIE_DOMAIN, false, true); } public function tryGetUser($nick, $passwd) { $result = $this->select(USERS_TABLE, 'user_id, nick', 'nick=\''.$nick.'\' AND password=\''.$passwd.'\'', '', 1); if (count($result) > 0 ) return $result[0]; else return array(); } public function isLogged() { if (count($this->userdata) > 0) return true; else return false; } public function getNick() { if ($this->isLogged()) return $this->userdata['nick']; else return null; } public function getRank() { if ($this->isLogged()) return $this->userdata['rank']; else return null; } public function getID() { if ($this->isLogged()) return $this->userdata['user_id']; else return null; } } ?>