IP Mapping in PHP Using MaxMind

 

In today’s tutorial we will be learning how to do some simple ip mapping using MaxMind’s database.  This is handy if for example your client wants different things to show for the east and west side of the country.  This could also be handy if you have a special going on in only one state, and you don’t want it to show up to visitors outside of that state.

Step 1 – Purchase MaxMind Queries

Head on over to MaxMind’s web services page – http://www.maxmind.com/app/web_services#city and purchase a few queries.  Take into consideration how many visitors your site will have because each visit will use up a query.  Once this is done you should have received a license key for which you can use in your PHP code.

Step 2 – Plan Your Code

This is a step a lot of PHP developers don’t do (myself included).  It is important, especially in larger projects to have a plan of attack.  Let’s create a new php file (ipmapping.php) and enter our plan of action in the comments.

<?php
// start session

// get the users ip address

// request ip information from our maxmind db

// store the received information in the session

// show link if conditions are met

?>

Step 3 – Write Code

Now begin coding the different sections.

<?php
// start session
session-start();

// get the users ip address
$current_ip= $_SERVER['REMOTE_ADDR'];

// request ip information from our maxmind db
$license_key = 'YOUR_LICENSE_KEY_HERE';
$query = "http://geoip3.maxmind.com/b?l=" . $license_key
. "&amp;i=" . $current_ip;

$url = parse_url($query);
$host = $url["host"];
$path = $url["path"] . "?" . $url["query"];
$timeout = 1;
$fp = fsockopen ($host, 80, $errno, $errstr, $timeout);

if ($fp) {
fputs ($fp, "GET $path HTTP/1.0\nHost: " . $host . "\n\n");

while (!feof($fp)) {
$buf .= fgets($fp, 128);
}

$lines = split("\n", $buf);
$data = $lines[count($lines)-1];
fclose($fp);
}
else {
# enter error handing code here
}

// store the received information in the session
$geo = explode(",",$data);
$current_country = $geo[0];
$current_region = $geo[1];
$current_city = $geo[2];
$current_lat = $geo[3];
$current_lon = $geo[4];

$_SESSION['current_region'] = $current_region;
$_SESSION['current_city'] = $current_city;
$_SESSION['current_lat'] = $current_lat;
$_SESSION['current_lon'] = $current_lon;
$_SESSION['current_country'] = $current_country;

// show link if conditions are met
?>
<?php if( $current_region == 'NY' ) : ?>
<a href="pdfs/new-york-brochure.pdf"  target="_blank">
Download New York Brochure  &raquo;
</a>
<?php endif; ?>

Step 4 – Test Code

One of the biggest headaches about IP Mapping is testing.  Just because it works for your IP doesn’t mean it works for everyone.  The rule of testing is to make sure you test all the different scenarios.  Use one ip from inside your test region, and one from outside.  Once you have these test ips put them into $current_region so it is static.  Once things are tested you can change $current_ip back to the way it was before.

Conclusion

So what do you think of our application?  Hopefully this is something you can use in your projects down the line.

No related posts.

Written by brenelz

Hello everyone, I'm Brenley Dueck or better known as Brenelz. I currently run my own business called Brenelz Web Solutions which focuses primary on winnipeg website design. The web technologies I most specialize in are CSS, jQuery, AJAX, PHP, and the MySQL database. Please make sure to follow me on twitter.

 

One Response to “IP Mapping in PHP Using MaxMind”

  1. Jason Says:

    January 9th, 2012 at 1:41 am

    Very nice! Thanks for sharing the code :)
    Do you know how to work this same code with the GeoIP.dat file? I also have Maxmind Country IP. And I use the GeoIP.dat file not the actual subscription for requests.
    I was trying to change the code for working with the GeoIP.dat but I keep getting errors.

    Thanks for any help.
    Jason.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 
connect with me!