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
{ 12 comments… read them below or Shout @ me! }
New blog post: City , Country and zipcode lookup via Ip address in PHP http://bit.ly/3fVVnk
This comment was originally posted on Twitter
Delicious: Lookup Langitude, longitude, City , Country from IP | PHP tutorials and Scripts Collection http://bit.ly/aSuwh
This comment was originally posted on Twitter
Lookup Langitude, longitude, City , Country from IP | PHP tutorials and Scripts Collection http://bit.ly/aSuwh
This comment was originally posted on Twitter
Lookup Langitude, longitude, City , Country from IP | PHP tutorials and Scripts Collection #php http://to.ly/nQy
This comment was originally posted on Twitter
Lookup Langitude, longitude, City , Country from IP | PHP tutorials and Scripts Collection http://bit.ly/U8JaG #php
This comment was originally posted on Twitter
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
Lookup Langitude, longitude, City , Country from IP | PHP tutorials and Scripts Collection http://bit.ly/U8JaG #php
This comment was originally posted on Twitter
City , Country and zipcode lookup via #IP address in #PHP http://bit.ly/8H883 #API
This comment was originally posted on Twitter
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.
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
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!
Glad it helped !
{ 1 trackback }