ip = ($_SERVER['REMOTE_ADDR']); if( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ){ $REMOTE_ADDR = $_SERVER['HTTP_X_FORWARDED_FOR']; $tmp_ip=explode(',',$REMOTE_ADDR); $this->ip =(htmlentities($tmp_ip[0],ENT_QUOTES,"UTF-8")); } if(isset($_SESSION[$OJ_NAME.'_'.'user_id'])) $this->ua = htmlentities($_SESSION[$OJ_NAME.'_'.'user_id'],ENT_QUOTES,"UTF-8"); else $this->ua ="guest"; $this->ua .= "@".htmlentities($_SERVER['HTTP_USER_AGENT'],ENT_QUOTES,"UTF-8"); $this->uri = ($_SERVER['PHP_SELF']); if(isset($_SERVER['HTTP_REFERER'])){ $this->refer = (htmlentities($_SERVER['HTTP_REFERER'],ENT_QUOTES,"UTF-8")); } $this->hash = md5(session_id().$this->ip); //check user existed! if($this->exist()){ //update databse $this->update(); }else if(!(strstr($this->ua,"bot")||strstr($this->ua,"spider"))){ //if none, add this record $this->addRecord(); } //clean the user who leave our site $this->clean(); } /** * * return all record! * * @return array */ function getAll() { $sql = 'SELECT * FROM online'; $ret = pdo_query($sql); return $ret; } /** * * return specfy record * @var string ip * @return object */ function getRecord($ip) { $sql = "SELECT * FROM online WHERE ip = ?"; $res = pdo_query($sql,$ip); if(count($res)){ $ret = ($res[0]); }else{ return false; } return $ret; } /** * * get total count * * @return int */ function get_num() { $sql = 'SELECT count(ip) as nums FROM online'; $res = pdo_query($sql); $ret = 0; if($res){ $ret = $res[0]; $ret = $ret['nums']; } return $ret; } /** * check the record exist * * @return boolean */ function exist() { $sql = "SELECT count(1) FROM online WHERE hash = ?"; $res = pdo_query($sql,$this->hash); return $res[0][0]; } /** * add a record * * @return void */ function addRecord() { $now = time(); $sql = "INSERT INTO online(hash, ip, ua, uri, refer, firsttime, lastmove) VALUES (?, ?,?, ?, ?, ?, ?)"; pdo_query($sql,$this->hash,$this->ip, $this->ua,$this->uri,$this->refer,$now,$now); } /** * update a record * * @return void */ function update() { $sql = "UPDATE online SET ua = ?, uri = ?, refer = ?, lastmove = ?, ip = ? WHERE hash = ? "; pdo_query($sql,$this->ua,$this->uri,$this->refer,time(),$this->ip,$this->hash); } /** * clean the duration user * * @return void */ function clean() { $sql = 'DELETE FROM online WHERE lastmove