IP to country converter – using MaxMind GeoIP API

We’ve covered the HostIP.info API for converting the IP address to a country and city of your visitor. But you may have noticed sometime HostIP.info does not convert everything and might have comparatively lower accuracy. But if you want better accuracy, HostIP.info also recommends you to get a commercial version such as MaxMind

MaxMind offers a number of options for you to buy their databases that provide better accuracy of IP to country conversion. And they allow easy integration in your PHP code, which you need to first download the PHP API, such as geoip.inc, and then just a few lines of code as follows, so you can map IP to country and city etc more precisely.

The example below shows how to use MaxMind GeoIP module to tell the visitor’s country and the file “/usr/local/share/GeoIP/GeoIP.dat” is the database file you’ve purchased from them.

<?php
 
// include the geoip.inc
include("geoip.inc");
 
// open the database file that you've purchased from MaxMind
$gi = geoip_open("/usr/local/share/GeoIP/GeoIP.dat",GEOIP_STANDARD);
 
// The remote client/visitor's IP address
$remote_ip = $_SERVER['REMOTE_ADDR'];
 
// now print the remote IP to country mapping to tell the client/visitor in which country that he/she is located 
echo "<p>You're from :".geoip_country_code_by_addr($gi,$remote_ip)."</p>" ;
 
geoip_close($gi);
 
?>

Enter your email address to subscribe our newsletter or feed for FREE:

Delivered by FeedBurner


Bookmark with:

[Delicious]    [Digg]    [Reddit]    [Facebook]    [StumbleUpon]

0 Responses to “IP to country converter – using MaxMind GeoIP API”


  1. No Comments

Leave a Reply

You must login to post a comment.