// this records your visiting surfer's IP address ...
$checkip = $_SERVER['REMOTE_ADDR'];
// or you could debug by using ... $checkip = "72.167.115.223"; ... which is my IP address that returned the sample data below
// for debugging purposes, this grabs the data but not in the form of an array, we will echo this to see all the variables available we can use
$geoData = file_get_contents('http://www.geoplugin.net/php.gp?ip='.$checkip);
// output the data to debug
echo $geoData."<br /><br />";
// for me the data below was output ...
/*
a:14:{s:14:"geoplugin_city";s:10:"Scottsdale";s:16:"geoplugin_region";s:2:"AZ";s:18:"geoplugin_areaCode";s:3:"480";s:17:"geoplugin_dmaCode";s:3:"753";s:21:"geoplugin_countryCode";s:2:"US";s:21:"geoplugin_countryName";s:13:"United States";s:23:"geoplugin_continentCode";s:2:"NA";s:18:"geoplugin_latitude";s:13:"33.6119003296";s:19:"geoplugin_longitude";s:14:"-111.890701294";s:20:"geoplugin_regionCode";s:2:"AZ";s:20:"geoplugin_regionName";s:7:"Arizona";s:22:"geoplugin_currencyCode";s:3:"USD";s:24:"geoplugin_currencySymbol";s:5:"$";s:27:"geoplugin_currencyConverter";d:1;}
*/
// lets grab the data a second time, but this time combine it into an array
$geo = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$checkip));
// out of all the data returned, this isolates the city
$city = $geo['geoplugin_city'];
// lets go one step further and also grab the country which is found in the debugging code returned above
$country = $geo['geoplugin_countryCode'];
// spitting it all out ...
echo "I am a visitor from $country country, from the city of $city";