I was bored today so I created a very simple and fast php script to plot the location for a given IP and display other IP information (such as lattitude, longitude, Country, State, Area code, ISP, etc.) on a Google Map using the Maxmind web service and the Google Maps API.
The link in the balloon points domaintools.com and does a whois lookup on the IP for detailed information on the ISP (internet service provider).
Here is a working demo (hard coded IP).
In order for this script to work on your site, you will need a MaxMind GeoIP City/ISP/Organization Web Service Key (50,000 lookups for $20.00) and a Google Maps Key (free). There are other IP to Lat/Long Web services out there but Maxmind is by far the most accurate (none are 100% accurate).
To render the location of an IP on the map, send the IP via the query string such as…
http://www.yourDomain.com/MaxmindGoogleMap/index.php?ip=98.195.83.50
Here is the script. Copy the contents to a .php file and edit the 2 keys. That’s it!
<?php ////////////////////////////////////////////////////////////////////////////////////// /////////////// Maxmind/Google Maps Mashup by Kevin Cornwell /////////////// /////////////// Version 1.0 /////////////// /////////////// http://www.kevincornwell.com /////////////// ///////////////////////////////////////////////////////////////////////////////////// // EDIT THE NEXT 2 KEYS $maxmind_license_key = "foo"; // Replace [foo] with your key. $google_maps_key = "bar"; // Replace [bar] with your key. $zoom = 4; // Default zoom level. $lat = 39.232; // Center on this location by default when no IP is passed. $long = -95.800; // Center on this location by default when no IP is passed. $ip = ""; $message = "Hello World"; // Default message when no IP is passed. if (isset($_REQUEST['ip'])) { $ip = $_REQUEST['ip']; $r = maxmind_lookup($ip); $message = "<table cellspacing=0><tr><td align='right'><b>IP:</b> </td><td><a href='http://whois.domaintools.com/{$ip}'>{$ip}</a></td></tr><tr><td align='right'><b>Country:</b> </td><td>{$r['country']}</td></tr><tr><td align='right'><b>Region:</b> </td><td>{$r['region']}</td></tr><tr><td align='right'><b>City:</b> </td><td>{$r['city']}</td></tr><tr><td align='right'><b>Zip:</b> </td><td>{$r['zip']}</td></tr><tr><td align='right'><b>Lat:</b> </td><td>{$r['lat']}</td></tr><tr><td align='right'><b>Long:</b> </td><td>{$r['long']}</td></tr><tr><td align='right'><b>Metro:</b> </td><td>{$r['metro_code']}</td></tr><tr><td align='right'><b>Area Code:</b> </td><td>{$r['area_code']}</td></tr><tr><td align='right'><b>ISP:</b> </td><td>{$r['isp']}</td></tr><tr><td align='right'><b>Org:</b> </td><td>{$r['org']}</td></tr></table>"; $lat = $r['lat']; $long = $r['long']; } function maxmind_lookup($ipaddress){ global $maxmind_license_key; // Returns:ISO 3166 Two-letter Country Code, Region Code, City, Postal Code, // Latitude, Longitude, Metropolitan Code, Area Code, ISP, Organization. $query = "http://maxmind.com:8010/f?l=" . $maxmind_license_key . "&i=" . $ipaddress; $url = parse_url($query); $host = $url["host"]; $path = $url["path"] . "?" . $url["query"]; $timeout = 1; $fp = fsockopen ($host, 8010, $errno, $errstr, $timeout) or die('Can not open connection to maxmind server.'); $buf = ""; 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); $arrMaxmind = split(",", $data); $arrMaxmind = str_replace("\"", "", $arrMaxmind); $arrMaxmind = str_replace(",", " ", $arrMaxmind); $r['country'] = $arrMaxmind[0]; $r['region'] = $arrMaxmind[1]; $r['city'] = $arrMaxmind[2]; $r['zip'] = $arrMaxmind[3]; $r['lat'] = $arrMaxmind[4]; $r['long'] = $arrMaxmind[5]; $r['metro_code'] = $arrMaxmind[6]; $r['area_code'] = $arrMaxmind[7]; $r['isp'] = $arrMaxmind[8]; $r['org'] = $arrMaxmind[9]; } else { return false; } return $r; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> <title><?php echo $ip ?></title> <script src="http://maps.google.com/maps?file=api&v=2&key=<?php echo $google_maps_key ?>" type="text/javascript"></script> <script type="text/javascript"> //<![CDATA[ function load() { if (GBrowserIsCompatible()) { var map = new GMap2(document.getElementById("map")); map.addControl(new GLargeMapControl()); map.addControl(new GMapTypeControl()); map.setCenter(new GLatLng(<?php echo $lat ?>, <?php echo $long ?>), <?php echo $zoom ?>); var marker = new GMarker(new GLatLng(<?php echo $lat ?>, <?php echo $long ?>)); marker.html = "<?php echo $message ?>"; GEvent.addListener(marker, 'click', function() {marker.openInfoWindowHtml(marker.html); }); map.addOverlay(marker); marker.openInfoWindowHtml(marker.html); } } //]]> </script> </head> <body onload="load()" onunload="GUnload()"> <div id="map" style="width:800px;height:600px"></div> </body> </html>
If you liked my script, post a thanks below.
:)
3 Comments
Leave a comment:


Sweet dood…This summer I’ll probably get back into a little webdev. We’ll still have our chats and what not, just not in person I assume. Lol
You can get the Web service to translate IP address to latitude and longitude with free 90 credits per month from http://www.fraudlabs.com/ip2location.aspx.
It is powered by the popular IP2Location geolocation technology.
Nice! This Rocks! How about using domain name instead of ip? (e.g. http://www.yahoo.com or yahoo.com) because in the online demo of GeoIP when i put yahoo.com, it will able to generate info. thanks!