Get country from ip-address

Get country name from ip-address


function countryCityFromIP($ipAddr)


//function to find country and city from IP address

//verify the IP address for the

ip2long($ipAddr)== -1 || ip2long($ipAddr) === false ? trigger_error(“Invalid IP”, E_USER_ERROR) : “”;

$ipDetail=array(); //initialize a blank array

//get the XML result from

$xml = file_get_contents(“”.$ipAddr);

//get the city name inside the node <gml:name> and </gml:name>


//assing the city name to the array


//get the country name inside the node <countryName> and </countryName>


//assign the country name to the $ipDetail array


//get the country name inside the node <countryName> and </countryName>


$ipDetail[‘country_code’]=$cc_match[1]; //assing the country code to array

//return the array containing city, country and country code

echo “Country Name:”.$ipDetail[‘country’].”City:”.$ipDetail[‘city’].”Country Code”.$ipDetail[‘country_code’];





Bansal Sumit

To seek challenging roles and to achieve excellence in the field of Open Source Web Technology with a profound spirit of commitment and dedication.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *