GeoIP: Getting the location of users via IP addresses
Since I have the locations of most of the oubs throughout Ireland Rate My Pub it would be interesting to be able to show the user where the nearest pub no him is. That is to scan his IP number get the longitude and latitude of this location and calculate the nearest pubs too him.
There are a number of solutions to this problem, the first I saw was hostip.info, which was ok at finding countires but since I am only dealing with one country I needed something with more detail. I needed something with a larger database of IP locations.
Two sites were recommended maxmind.com and ipligence.com, since maxmind offers a “lite” version for free I decided to use this model. The process is simple the GeoLiteCity.dat, and the geoipcity.inc file need to be downloaded from maxmind.com and uploaded to your server. The following code can then be used to calculate data:
include(”geoipcity.inc”);
$gi = geoip_open(”GeoLiteCity.dat”,GEOIP_STANDARD);
$record = geoip_record_by_addr($gi,”159.215.236.21″);
print $record->city . “\n”;
print $record->country_code . ” ” . $record->country_code3 . ” ” . $record->country_name . “\n”;
print $record->region . ” ” . $GEOIP_REGION_NAME[$record->country_code][$record->region] . “\n”;
print $record->postal_code . “\n”;
print $record->latitude . “\n”;
print $record->longitude . “\n”;
print $record->dma_code . “\n”;
print $record->area_code . “\n”;
geoip_close($gi);
?>
Combining this code with the users IP address should give the langitude and latitude of the users location. This Lite version will be tested before a decision about purchasing the full version will be made.