You can use external API's like geoplugin.net
$xml = simplexml_load_file("http://www.geoplugin.net/xml.gp?ip=76.109.14.196");
echo $xml->geoplugin_countryName ;
Output Country
United States
Full XML Response
<geoPlugin>
<geoplugin_request>76.109.14.196</geoplugin_request>
<geoplugin_status>200</geoplugin_status>
<geoplugin_city>West Palm Beach</geoplugin_city>
<geoplugin_region>FL</geoplugin_region>
<geoplugin_areaCode>561</geoplugin_areaCode>
<geoplugin_dmaCode>548</geoplugin_dmaCode>
<geoplugin_countryCode>US</geoplugin_countryCode>
<geoplugin_countryName>United States</geoplugin_countryName>
<geoplugin_continentCode>NA</geoplugin_continentCode>
<geoplugin_latitude>26.761600494385</geoplugin_latitude>
<geoplugin_longitude>-80.091598510742</geoplugin_longitude>
<geoplugin_regionCode>FL</geoplugin_regionCode>
<geoplugin_regionName>Florida</geoplugin_regionName>
<geoplugin_currencyCode>USD</geoplugin_currencyCode>
<geoplugin_currencySymbol>$</geoplugin_currencySymbol>
<geoplugin_currencyConverter>1</geoplugin_currencyConverter>
</geoPlugin>
Simple Function to Get IP
function getIP() {
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (! empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}