<?php
require('functions.php');
unlock_all_files();
$ip = $_SERVER['REMOTE_ADDR'];
$data = unserialize(file_get_contents("liveupdate.txt"));
if(!isset($data[$ip]["location"]) AND !isset($data[$ip]["start"])) {
$location = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.substr($_SERVER['REMOTE_ADDR'], 0, -2)."00"));
$data[$ip]["location"] = $location['geoplugin_countryName'];
echo $data[$ip]['location'];
$data[$ip]["start"] = time();
}
$data[$ip]["timestamp"] = time();
$data[$ip]["useragent"] = $_SERVER["HTTP_USER_AGENT"];
$data[$ip]["actions"] = $_POST["actions"];
$data[$ip]["referrer"] = $_POST["referrer"];
$data[$ip]['site'] = $_SERVER['HTTP_REFERER'];
foreach(array_keys($data) AS $key) {
if(intval($data[$key]["timestamp"] + 12) <= time()) {
unset($data[$key]);
}
}
file_put_contents("liveupdate.txt",serialize($data));
$log = file('log.txt');
$count = count($log);
$reversed = array_reverse($log);
foreach($reversed AS $line) {
	$line = explode(', ',$line);
	if($line[1]==substr($_SERVER['REMOTE_ADDR'],0,-2).'00' AND !isset($exec)) {
		$line[8] = $_POST['time_on_site'];
		$line[13] = $_POST['actions'];
		$count--;
		$exec = true;
	}
	$newfile[] = implode(', ',$line);
}
file_put_contents('log.txt',implode('',array_reverse($newfile)));
?>