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.
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.
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 ?>
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
. "&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 »
</a>
<?php endif; ?>
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.
So what do you think of our application? Hopefully this is something you can use in your projects down the line.
No related posts.
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.
Twitter
Follow me on Twitter to keep up to date!
RSS Feed
Keep up with all of our updates by subscribing to our RSS feed!
FaceBook
Join our group on Facebook and become a fan of us!