Code below is saving the data in logfile.htm. But only the IP is written in de file.
Country I don’t get is. What is wrong?
<?php
function logIP()
{
$ipLog="logfile.htm"; // Your logfiles name here (.txt or .html extensions ok)
// IP logging function by Dave Lauderdale
// Originally published at: www.digi-dl.com
$register_globals = (bool) ini_get('register_gobals');
if ($register_globals) $ip = getenv(REMOTE_ADDR);
else $ip = $_SERVER['REMOTE_ADDR'];
$ip_info = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));
if($ip_info && $ip_info->geoplugin_countryName != null){
echo 'Country = '.$ip_info->geoplugin_countryName.'<br/>';
echo 'Country Code = '.$ip_info->geoplugin_countryCode.'<br/>';
echo 'City = '.$ip_info->geoplugin_city.'<br/>';
echo 'Region = '.$ip_info->geoplugin_region.'<br/>';
echo 'Latitude = '.$ip_info->geoplugin_latitude.'<br/>';
echo 'Longitude = '.$ip_info->geoplugin_longitude.'<br/>';
echo 'Timezone = '.$ip_info->geoplugin_timezone.'<br/>';
echo 'Continent Code = '.$ip_info->geoplugin_continentCode.'<br/>';
echo 'Continent Name = '.$ip_info->geoplugin_continentName.'<br/>';
echo 'Timezone = '.$ip_info->geoplugin_timezone.'<br/>';
echo 'Currency Code = '.$ip_info->geoplugin_currencyCode;
}
$date=date ("l dS of F Y h:i:s A");
$log=fopen("$ipLog", "a+");
if (preg_match("/\\bhtm\\b/i", $ipLog) || preg_match("/\\bhtml\\b/i", $ipLog))
{
fputs($log, "Logged IP address: $ip - Country = $ip_info->geoplugin_countryName - Date logged: $date<br>");
}
else fputs($log, "Logged IP address: $ip - Country = $ip_info->geoplugin_countryName - Date logged: $date\
");
fclose($log);
}
// Place the below function call wherever you want the script to fire.
logIp();
?>