Saving data with PHP

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();


?>

Well, I would guess you are not getting those values or you are not decoding them correctly.
First, add error checking commands to the top of the page. Remove the @ suppression of errors in your file_get_contents line and see if there is a real error or not. Otherwise, you can display the json info and see what is inside of it. Try debugging it that way first and let us know…

I removed. But still not working

What error are you getting? Put these lines at the top of your page and tell us the error you receive.
Sometimes the error is posted on the screen, sometimes they are hidden in your server’s log system.
If a local system like Wamp, you can view the errors in the folder named “logs” and if online, you can
view them using the server’s control panel.

We need to know what error you are receiving

As far as this JSON plugin site goes, I tested it using my IP and some others. It did not get the correct CITY on most of my tests. But, it did get the COUNTRY and STATE correct on the limited tests I ran.
On shared servers you might get an incorrect response. Also, I see you check IF($ip_info) which checks if the item has valid data. But, since json_decode data is an object, you would not validate it this way.
You should use IF (IS_OBJECT($ip_info) OR even ISSET, I think just comparing the variable will not work.

Sorry for late answer, But it is fixed. Below the code.

<?php
$ip =  $_SERVER['REMOTE_ADDR']; 

function logIP()
{
     $ipLog="logmain.htm"; // Your logfiles name here (.txt or .html extensions ok)

     $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)); 

     $date=date ("l dS 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();

?>
Sponsor our Newsletter | Privacy Policy | Terms of Service