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

Be Sociable, Share!

{ 18 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

Reply

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

Reply

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

Reply

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

Reply

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

Reply

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

Reply

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

Reply

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

Reply

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.

Reply

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

Reply

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

Reply

Php Ninza September 28, 2009 at 4:31 am

Glad it helped ! :)

Reply

zip code files March 8, 2011 at 6:50 am

This is nice blog and i like this blog. you want to zip code data and maps here available and download immediately. We hope zip code data and maps much useful.

Reply

Learn PHP July 2, 2011 at 6:35 am

Zip code data download link is in the post. I hope that is what you asked.

Reply

proxy April 24, 2011 at 3:32 am

thanks,it can help me.

Reply

Saurabh Kumar Singh November 29, 2011 at 10:11 am

Its really very help full for me.

Reply

Ran March 1, 2012 at 1:35 am

well, postcode or zip code was the main thing that I wanted to achieve and that’s inconsistent in this script finally huh …

wasted my couple of hours.

Reply

Learn PHP March 1, 2012 at 2:55 pm

@Ranjeet Unfortunately can’t do much about it. I guess you’ve a paid version too. Check that out.

Reply

Leave a Comment

{ 1 trackback }

Previous post:

Next post: