City , Country and zipcode lookup via Ip address in PHP


by Php Ninza on July 31, 2009

A lot of times i have seen newbies and even PHP pros struggling to find any method of implementing zipcode and country lookup !

This can be easily achieved with the help of Maxmind Geo API, They provide a exhaustive DB lookup functionality of IP adress . Which can be further translated into City, Country and Zipcode information ! . This API prove to be very useful in the web apps which require providing user specific content to the users.

For the usage you simply need to download the GeoLiteCity database (Updated monthly) from this link.Also download three more files namely geoipcity.inc and geoipregionvars.php and put all three files in a folder name inc. Change PATH TO YOUR ROOT PUBLIC_HTML FOLDER to your server public_html path.

Once all files are in place , Simply use the code given below to fetch the city, country , zipcode , langitude and longitude information from the GeoLiteCity database.


include("inc/geoipcity.inc");
include("inc/geoipregionvars.php");
$IP = $_SERVER['REMOTE_ADDR'];
$gi = geoip_open("PATH TO YOUR ROOT PUBLIC_HTML FOLDER/inc/GeoLiteCity.dat",GEOIP_STANDARD);
$record = geoip_record_by_addr($gi,$IP);
$city =  $record->city;
$country = $record->country_name;
$latitude = $record->latitude;
$longitude = $record->longitude;
geoip_close($gi);

The function geoip_open() opens the database for reading and geoip_record_by_addr() uses the database and the ip to generate the record .

All information is embed into the variable $record in above given example. Which can be fetched namely via

<?php
print_r ($record);
?>

All variable name are self-explanatory to include the desired data !

Let us know if you encounter any issues in deploying this tutorial

Alternatively if you are making a google mashup, Try out this excellent book that i used for my mashups

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay

{ 12 comments… read them below or Shout @ me! }

phpcollection August 1, 2009 at 6:31 pm

New blog post: City , Country and zipcode lookup via Ip address in PHP http://bit.ly/3fVVnk

This comment was originally posted on Twitter

Taikochu August 3, 2009 at 7:15 pm

Delicious: Lookup Langitude, longitude, City , Country from IP | PHP tutorials and Scripts Collection http://bit.ly/aSuwh

This comment was originally posted on Twitter

phpizer August 3, 2009 at 7:15 pm

Lookup Langitude, longitude, City , Country from IP | PHP tutorials and Scripts Collection http://bit.ly/aSuwh

This comment was originally posted on Twitter

jonasl August 3, 2009 at 8:46 pm

Lookup Langitude, longitude, City , Country from IP | PHP tutorials and Scripts Collection #php http://to.ly/nQy

This comment was originally posted on Twitter

web2feed August 4, 2009 at 2:33 am

Lookup Langitude, longitude, City , Country from IP | PHP tutorials and Scripts Collection http://bit.ly/U8JaG #php

This comment was originally posted on Twitter

Php_Shell August 4, 2009 at 5:05 am

Lookup Langitude, longitude, City , Country from IP | PHP tutorials and Scripts Collection #php http://to.ly/nQy (via @jonasl)

This comment was originally posted on Twitter

paulmilford August 4, 2009 at 8:01 am

Lookup Langitude, longitude, City , Country from IP | PHP tutorials and Scripts Collection http://bit.ly/U8JaG #php

This comment was originally posted on Twitter

revaxarts August 6, 2009 at 7:27 am

City , Country and zipcode lookup via #IP address in #PHP http://bit.ly/8H883 #API

This comment was originally posted on Twitter

Kengi September 26, 2009 at 1:40 pm

I ran into this great post yesterday, and it ALMOST worked for what I needed. All the other variables like city and country returned fine, but did you have trouble getting the postal_code variable to work? It just returns blank for me. Unfortunately, all i need is the postal_code.

Php Ninza September 27, 2009 at 2:11 am

Postal code is fetched in the $record variable , However data is little inconsistent for postal code. Not all postal codes are fetched.

Hope it helps

Kengi September 27, 2009 at 7:16 pm

now i remember reading about the inconsistency. I did find a solution combining other zipcode data. Still gotta thank you for this post for I would have had to drop atleast $300 plus monthly fees on the services out there. Kudos! :D

Php Ninza September 28, 2009 at 4:31 am

Glad it helped ! :)

Leave a Comment

{ 1 trackback }

Previous post:

Next post: