Created
March 11, 2013 20:30
-
-
Save iwek/5137461 to your computer and use it in GitHub Desktop.
Retrieve Location from MaxMind Database in MySQL with PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//check the POST | |
$ips = $_POST["ips"]; | |
if (empty($ips)) { | |
echo 'Please provide ips.'; | |
} else { | |
//connect to mysql and select the maxmind database | |
$con = mysql_connect("localhost","root","root"); | |
if (!$con){ | |
die('Could not connect: ' . mysql_error()); | |
} | |
mysql_select_db("maxmind", $con); | |
//blank array setup to store all lat and long coords | |
$finalarray = array(); | |
//turn ip string into array so we can loop through it | |
$iparray = explode(",", $ips); | |
//loop through each ip | |
foreach($iparray as $ip){ | |
//our mysql query | |
$query = "SELECT latitude,longitude FROM location WHERE locid = ( SELECT locid FROM blocks WHERE INET_ATON('". $ip ."') BETWEEN startIPNum AND endIPNum);"; | |
$result = mysql_query($query); | |
if ($result) { | |
$row = mysql_fetch_row($result); | |
//create an array of ip geo coords for one result | |
$geoarray = array($row[0], $row[1]); | |
//push array of individual geo coords into main geo location array above | |
array_push($finalarray, $geoarray); | |
} | |
} | |
//return the final multidimensional array as JSON | |
$return = json_encode($finalarray); | |
echo $return; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment