$onlinetime="900"; // 15 minutes $base_dir=$_SERVER['DOCUMENT_ROOT']; $ipfile=$base_dir . "/counter/ipfile.txt"; $datafile=$base_dir . "/counter/datafile.txt"; $writefile=false; $ignore=true; $date = date("d-m-Y"); if ( isset($_SERVER["REMOTE_ADDR"]) ) { $ip_address=$_SERVER["REMOTE_ADDR"] . ' '; } else if ( isset($_SERVER["HTTP_X_FORWARDED_FOR"]) ) { $ip_address=$_SERVER["HTTP_X_FORWARDED_FOR"] . ' '; } else if ( isset($_SERVER["HTTP_CLIENT_IP"]) ) { $ip_address=$_SERVER["HTTP_CLIENT_IP"] . ' '; } //put your own IP address in here so that its not counted //if (trim($ip_address) == '123.45.67.89') { //exit; //} // open file and read in data $file = fopen($datafile,"r"); $data=fgets($file); fclose($file); list ($today, $totalvisitors, $online, $visitorstoday, $visitorsyesterday, $twodaysago, $threedaysago, $mostvisitors, $mostvisitorsdate, $mostonline, $mostonlinedate, $hits) = explode("|", $data); $hits++; $lines = file($ipfile); if ($lines==null) { $ignore = true; } if ($today==null) { $today = $date; } if ($mostvisitors==null) { $mostvisitors = 0; } $x=0; // Loop through our array foreach ($lines as $line) { $pieces = explode("|", $line); // Check to see if IP matches or time is over if ($pieces[0]==$ip_address){ $ignore=false; } //remove any lines that are over time if (time()-$pieces[1] > $onlinetime){ unset($lines[$x]); if ($lines==null) { $ignore = true; } else { $writelot="ok"; } } $x++; } // write complete unaltered file if ($writelot=="ok"){ $fp = fopen($ipfile, 'w'); $total = count($lines); // $total++; if (flock($fp, LOCK_EX)) { for ($i = 0; $i <= $total; $i++){ fwrite($fp,$lines[$i]); } flock($fp, LOCK_UN); } fclose ($fp); }else{ // append new ip to file if ($ignore==true){ $fp = fopen($ipfile, 'a'); $total = count($lines); $total++; if (flock($fp, LOCK_EX)) { fwrite($fp, $ip_address . "|" . time() . "\r\n"); flock($fp, LOCK_UN); } fclose($fp); }else{$total = $online;} } //check for total visitors online $online = $total; if ($online ==""){$online=0;} if ($mostonline<1){ $mostonline=1; $today1 = date('d-m-Y'); $mostonlinedate=$today1; } //most online date if ($online >= $mostonline){ $mostonline=$online; $today1 = date('d-m-Y'); $mostonlinedate=$today1; } //visitors if ($ignore==true){ $totalvisitors++; } if ($today == $date){ if ($ignore==true){ $visitorstoday++; } if ($visitorstoday >= $mostvisitors){ $mostvisitors=$visitorstoday; $today1 = date('d-m-Y'); $mostvisitorsdate=$today1; } }else{ // change date if required $today=$date; $threedaysago=$twodaysago; $twodaysago=$visitorsyesterday; $visitorsyesterday=$visitorstoday; $visitorstoday=1; if ($ignore==false) { $totalvisitors++; } } $fp = fopen($datafile, 'w'); if (flock($fp, LOCK_EX)) { fwrite($fp, $today."|".$totalvisitors."|".$online."|".$visitorstoday."|".$visitorsyesterday."|".$twodaysago."|".$threedaysago."|".$mostvisitors."|".$mostvisitorsdate."|".$mostonline."|".$mostonlinedate."|".$hits); flock($fp, LOCK_UN); } fclose($fp); ?>