Getting country , city name from IP address in PHP

Posted on July 17, 2008 
Filed Under how-to, php, tips and technique, web services

Yesterday, miaki asked me how can we get the country name from the IP address in PHP. Today, I’ve come up with the answer of this question. I’ve used the API from hostip.info to fetch the country name , city name and country code from the given IP address. I’ve mad this function in PHP which uses XML response from hostip.info and extracted country name, city name and country code using regular expression.

Function to return country, city name from IP address in PHP

function countryCityFromIP($ipAddr)
{
//function to find country and city from IP address
//Developed by Roshan Bhattarai http://roshanbh.com.np

//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 hostip.info
$xml = file_get_contents("http://api.hostip.info/?ip=".$ipAddr);

//get the city name inside the node <gml:name> and </gml:name>
preg_match("@<Hostip>(\s)*<gml:name>(.*?)</gml:name>@si",$xml,$match);

//assing the city name to the array
$ipDetail['city']=$match[2]; 

//get the country name inside the node <countryName> and </countryName>
preg_match("@<countryName>(.*?)</countryName>@si",$xml,$matches);

//assign the country name to the $ipDetail array
$ipDetail['country']=$matches[1];

//get the country name inside the node <countryName> and </countryName>
preg_match("@<countryAbbrev>(.*?)</countryAbbrev>@si",$xml,$cc_match);
$ipDetail['country_code']=$cc_match[1]; //assing the country code to array

//return the array containing city, country and country code
return $ipDetail;

}

Download Source Code

As you can see, I’ve documented all the PHP code and I don’t think I need explain anymore about that code. Just notice that, this function returns the array containg three key elements “country” , “city” and “country_code”. Each elements have the value of city, country and country code.

Now, look the the how we can use the above function in PHP,

$IPDetail=countryCityFromIP('12.215.42.19');
echo $IPDetail['country']; //country of that IP address
echo $IPDetail['city']; //outputs the IP detail of the city

Notice that the above PHP function returns the array containing the country , city and country code from IP Address and we can use them in PHP. If you want to know how to get IP address in PHP, you can check this post how you can get real IP address in PHP.

Popularity: 32% [?]

Enter your email address and get recent tutorials, tips, tricks and scripts of PHP, Ajax, JavaScript and CSS directly delivered to you email inbox:

Follow me on twitter at http://twitter.com/roshanbh.

Related Posts

» Change dropdown list (options) values from database with ajax and php
» Populate triple drop down list from database using Ajax and PHP
» 5 useful Google search tips you might now know
» Getting real IP address in PHP

Comments

18 Responses to “Getting country , city name from IP address in PHP”

  1. subesh on July 17th, 2008 11:31 am
  2. brsyuksel on July 18th, 2008 8:43 am

    http://hido.net/ip2country.txt
    http://hido.net/ip2country.phps

    ;) It can output only Country name but running on your server. You don’t need other service. ( Sorry my bad english :) )

  3. Roshan on July 18th, 2008 9:39 am

    @brsyuksel …thanks for sharing that with us…

  4. Krasimir Kazakov on July 18th, 2008 12:45 pm
  5. Hugh Fletcher on July 18th, 2008 3:30 pm

    I wrote a script a while back using the html that hostip returned, thanks for the easy read on an xml version. For those that find hostip.info returns inaccurate info, I suggest http://maxmind.com as an very accurate but pay alternative.

  6. Roshan on July 18th, 2008 3:38 pm

    @Krasimir - You’ve to pay some bucks to maxmind for more accurate information retrieval

  7. Someone on July 18th, 2008 5:26 pm

    This /isn’t/ using php to derive country names. This is only a method of parsing xml using php for a site that may or may not be operational tomorrow. I’d see this as a fairly temporary solution, at best.

  8. Roshan on July 19th, 2008 4:58 am

    @someone - yes this a only method to consume the web services using PHP and this is the best free option you can get in internet to find the city and country from IP using PHP.

    For reliability you can purchase the expensive database and use it in your application.

  9. Shakeel Shrestha on July 20th, 2008 7:13 am

    Our college IP in Pokhara is 202.79.63.59
    When I run this script using this IP, it shows Hydrabad, India…..

    College get the internet from wlink, pokhara center.

    Why is it so?

  10. Roshan on July 20th, 2008 8:35 am

    ya seems that their API is returning wrong value for this ip address 202.79.63.59. Hostip.info is returning Hydarabad, India but when I checked the same from ip2location.com it is showing it is from Nepal.

    http://www.ip2location.com/202.79.63.59

  11. Hem on July 21st, 2008 4:27 am

    I have done some this and that to show country , browser info and OS ( plugins ) in comment list .this one is good too

  12. seb on July 21st, 2008 5:46 am

    Yeah the problem with this approach is that the API looks up the location of the company that owns the netblock, or so it seems.

    The city/province returned when I look up my cable modem is the city/province where my ISP is headquartered (and that’s not where I am, it’s in a different province elsewhere in the country).

    Not super useful.

  13. Roshan on July 21st, 2008 9:40 am

    It’s not the fault of their API if you’re sending wrong IP address. $_SERVER['REMOTE_ADDR'] always returns the IP address of proxy server not the IP address of the computer.

    You better have to get the HTTP_X_FORWARDED_FOR and HTTP_CLIENT_IP header values…

    hope this helps

  14. stratosg on July 23rd, 2008 5:57 pm

    this is a nice find and script. the inaccuracy may be also caused because their database can be a bit old…

  15. maya on July 31st, 2008 2:12 am

    Hello it was a great article and i like your blog! keep up the good work…

  16. Ireland web z on August 3rd, 2008 9:21 pm

    Hi, i’m programing dating site and this is cool working script. Combined with get real ip - amazing. Great job. Thank you!!!

  17. Alex on August 7th, 2008 11:11 am

    Hi,

    Ok, we found the details about ip address, can any help me to know about the mechine(computer name) name using ip

  18. Roshan on August 7th, 2008 5:01 pm

    for this you can use gethostbyaddr() function of PHP

Leave a Reply