<?php 
	
function getUserIP()
{
    $client  = @$_SERVER['HTTP_CLIENT_IP'];
    $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
    $remote  = $_SERVER['REMOTE_ADDR'];

    if(filter_var($client, FILTER_VALIDATE_IP))
    {
        $ip = $client;
    }
    elseif(filter_var($forward, FILTER_VALIDATE_IP))
    {
        $ip = $forward;
    }
    else
    {
        $ip = $remote;
    }

    return $ip;
}


/*
function console_log($output, $with_script_tags = true) {
    $js_code = 'console.log(' . json_encode($output, JSON_HEX_TAG) . 
');';
    if ($with_script_tags) {
        $js_code = '<script>' . $js_code . '</script>';
    }
    echo $js_code;
}
*/


$download = './TimelimeTrial.zip'; // the link to your download file

date_default_timezone_set('Europe/Berlin');
$date = date('Y/m/d H:i:s', time());

$user_ip = getUserIP();
$details = json_decode(file_get_contents("http://ipinfo.io/{$user_ip}/json"));

$previous_file_data = file_get_contents('./count/counter.txt');

$previous_date = explode(' ',trim($previous_file_data))[0];
$current_date = explode(' ',trim($date))[0];

$previous_ip = explode(',',trim($previous_file_data))[4];

$eol;

if (strcmp($current_date, $previous_date) !== 0) {
	$eol = PHP_EOL.PHP_EOL;
} else {
	$eol = PHP_EOL;
}

// console_log($user_ip == $previous_ip);
// console_log("here2");

if ($user_ip == $previous_ip) {
	
	$previous_lines_array = explode("\n", $previous_file_data);

	$first_line = $previous_lines_array[0];
	array_shift($previous_lines_array);  // drop the first element
	
	$file_data = $first_line." +1"."\n".join("\n",$previous_lines_array);
	file_put_contents('./count/counter.txt', $file_data);
} else {
	$file_data = $date.",".$details->country.",".$details->region.",".$details->city.",".$user_ip.",".substr($download, 2).$eol;
	$file_data .= $previous_file_data;
	file_put_contents('./count/counter.txt', $file_data);
}


 
##fclose($fh); // close count file 
header("Location: $download"); // get download 
   
?>