Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home PHP PHP IP Address To Country City Region Latitude And Longitude

PHP IP Address To Country City Region Latitude And Longitude

In this post, I’ll show How to lookup the country, region, city, postal code, latitude, and longitude by IP Address. There are some free online tools to get those information from an IP Address. Today, I’ll use one of them, Maxmind as a demonstration.

First, you need to download the latest GeoLite City database in Binary Format from Maxmind. It’s FREE, updated monthly and accuracy up to 99.5%. You also need to download their PHP API to parse the Binary database and get information from an input IP Address.

1. PHP sample

<?php
 
include("geoipcity.inc");
include("geoipregionvars.php");
 
$gi = geoip_open("M:/Web/htdocs/maxmind/GeoLiteCity.dat",GEOIP_STANDARD);
 
$record = geoip_record_by_addr($gi,"67.227.221.249");
 
print "country_code: " . $record->country_code . "<br>";
print "country_code3: " . $record->country_code3 . "<br>";
print "country_name: " . $record->country_name . "<br>";
print "region: " . $record->region . "<br>";
print "region_name: " . $GEOIP_REGION_NAME[$record->country_code][$record->region] . "<br>";
print "city: " . $record->city . "<br>";
print "postal_code: " . $record->postal_code . "<br>";
print "latitude: " . $record->latitude . "<br>";
print "longitude: " . $record->longitude . "<br>";
print "metro_code: " . $record->metro_code . "<br>";
print "area_code: " . $record->area_code . "<br>";
 
geoip_close($gi);
 
?>

<?php include("geoipcity.inc"); include("geoipregionvars.php"); $gi = geoip_open("M:/Web/htdocs/maxmind/GeoLiteCity.dat",GEOIP_STANDARD); $record = geoip_record_by_addr($gi,"67.227.221.249"); print "country_code: " . $record->country_code . "<br>"; print "country_code3: " . $record->country_code3 . "<br>"; print "country_name: " . $record->country_name . "<br>"; print "region: " . $record->region . "<br>"; print "region_name: " . $GEOIP_REGION_NAME[$record->country_code][$record->region] . "<br>"; print "city: " . $record->city . "<br>"; print "postal_code: " . $record->postal_code . "<br>"; print "latitude: " . $record->latitude . "<br>"; print "longitude: " . $record->longitude . "<br>"; print "metro_code: " . $record->metro_code . "<br>"; print "area_code: " . $record->area_code . "<br>"; geoip_close($gi); ?>

2. Output

country_code: US
country_code3: USA
country_name: United States
region: MI
region_name: Michigan
city: Lansing
postal_code: 48917
latitude: 42.7257
longitude: -84.636
metro_code: 551
area_code: 517

country_code: US country_code3: USA country_name: United States region: MI region_name: Michigan city: Lansing postal_code: 48917 latitude: 42.7257 longitude: -84.636 metro_code: 551 area_code: 517

Download the demonstration source code above. It’s include the sample file, all included files and GeoLiteCity.dat.

Jun 27, 2011Hoan Huynh
Free Software To Monitor and Log Directories Files Changed On Windows 2000 XP 2003 Vista 7 Server 2008Delete WordPress Revision Posts, Its Relationships And Meta Data
You Might Also Like:
  • Get WOEID Of A City Name From IP Address With PHP
  • How To Track Website With Multiple Google Analytisc Accounts
  • Free Online Tools Get IP Address Location, Organization, ISP, Hostname, Country
  • Facebook Like Button And Recommend Button With fb:like, iframe and html5
  • PHP validate ip address by using regular expression
  • Validate Email Address Format Using PHP Regular Expression preg_match
  • PHP Ip to Country
  • WOEID Of Eight Capital Cities In Australia
  • CSharp Validate Email Address Function With Regular Expression
  • Websites Hosted On A Web Server With IP Address
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

11 years ago PHPGeoLiteCity, ip address, Maxmind317
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
24,550 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
21,886 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
17,740 views
JQuery Allow only numeric characters or only alphabet characters in textbox
15,063 views
C# Read Json From URL And Parse/Deserialize Json
11,795 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Things to Learn about Installingderm Loan Type S
  • Online Photo Editor – Free Photoediting Software
  • A Guide to Finding the Best Paper Sellers
  • Photoediting in Home Isn’t Hard to Do!

  • Free Photo Editor Online
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (27)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (647)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development