PHP: Get visitor’s location, city, country from IP address

Ip to Location

Many applications need to get information about the geographical location of the user/visitor on their websites. This can be done in many ways. There are online services like ipinfodb and geoio that provide web based api to get the geolocation information about a given ip address. Services like Maxmind geoip provide free database that can be installed on a website and used directly without the need to contact any third party.

Web based

IPInfoDB provides a simple API to get the location of a visitor from his IP address. You need to first register and get an API Key.

Then this code can be used :

$url = "$your_key&ip=$ip&format=json";
$d = file_get_contents($url);
$data = json_decode($d , true);
"statusCode" : "OK",
"statusMessage" : "",
"ipAddress" : "",
"countryCode" : "US",
"countryName" : "UNITED STATES",
"regionName" : "CALIFORNIA",
"cityName" : "MOUNTAIN VIEW",
"zipCode" : "94043",
"latitude" : "37.3956",
"longitude" : "-122.076",
"timeZone" : "-08:00"

$info = array(
	'ip' => $data['ipAddress'] ,
	'country_code' => $data['countryCode'] ,
	'country_name' => $data['countryName'] ,
	'region_name' => $data['regionName'] ,
	'city' => $data['cityName'] ,
	'zip_code' => $data['zipCode'] ,
	'latitude' => $data['latitude'] ,
	'longitude' => $data['longitude'] ,
	'time_zone' => $data['timeZone'] ,
return $info;

IPinfodb supports json and xml formats for data output.
The above code can be seen in action at

Another website GeoIO provides a similar API. You need to first register and get your code.

Then you can code like this :

$url = "$your_key&qt=geoip&d=comma&q=$ip";
$d = file_get_contents($url);
$data = explode(',' , $d);
	Kochi,Kerala,India,Nib (national Internet Backbone),9.9667,76.2333,IN
$info = array(
	'country_code' => $data[6] ,
	'country_name' => $data[2] ,
	'region_name' => $data[1] ,
	'city' => $data[0] ,
	'latitude' => $data[4] ,
	'longitude' => $data[5] ,
	'isp' => $data[3] ,

Geoio provides data in delimited format like comma delimited , pipe delimited and so on.


It should be kept in mind, that the data provided by 'free' services is not guaranteed to be accurate. The accuracy level changes over factors. For example the paid version of the same service offers higher accuracy. The accuracy also varies from vendor to vendor as well. Also the accuracy of the same database falls over a period of time, since ip allocations made by various isps and worldwide internet registries change constantly.

Last Updated On : 2nd May 2009

Subscribe to get updates delivered to your inbox

About Silver Moon

Php developer, blogger and Linux enthusiast. He can be reached at [email protected]. Or find him on

10 Comments + Add Comment

  • How about the variable $your_key? Thank you.

  • its not working… can you help me?? my mail id [email protected]

  • If you need accurate geo location try .You can also use their service to deliver geo targeted images, content and redirects without any coding. There is no need to install any databases or write any code to get locationl

    • ok

  • Nice Its working, Thanks

  • thanx very much :D

  • thanks nice code.

  • Getting an Ip-Address of an city using the website It is possible in many websites.I have recently used the site the informations very fast,free of cost also..

    • pls provide me code for get location of state and coutry in cakephp

      • how to get city country and state.

Leave a comment