City & Country Location Finding with GeoIP
January 14th, 2008Following on from a previous post, “PHP Geo-IP Location in 4 lines”, I’ve had a look at stalking people a bit more. Here’s how to lookup their location specific to city.
As usual you will need the MaxMind GeoLite City binary database.
Here’s the code
<? include("/usr/local/share-php/geoipcity.inc"); include("/usr/local/share-php/geoipregionvars.php"); $gi = geoip_open("/usr/local/share-php/GeoLiteCity.dat",GEOIP_STANDARD); $record = geoip_record_by_addr($gi,$_SERVER['REMOTE_ADDR']); echo "Country Code: ".$record->country_code . " " . $record->country_code3 . " " . $record->country_name . "<br />"; echo "Region: ".$record->region."<br />"; echo "City: ".$GEOIP_REGION_NAME[$record->country_code][$record->region]." ".$record->city."<br />"; echo "Post Code: ".$record->postal_code."<br />"; echo "Lat: ". $record->latitude."<br />"; echo "Long: ".$record->longitude."<br />"; geoip_close($gi); ?>
You should get results, similar to
Country Code: GB GBR United Kingdom
Region: M6
City: South Gloucestershire Hill
Post Code:
Lat: 51.6514
Long: -2.5156
GB GBR United Kingdom Hill 51.6514 -2.5156
* Remember to keep your MaxMind databases upto date!
| Bookmark it del.icio.us | Reddit | Slashdot | Digg | Facebook | Technorati | Google | StumbleUpon | Window Live | Tailrank | Furl | Propeller | Yahoo |
Was this post useful to you? Let me know, buy me a beer!
Alternatively, if you're feeling impecunious, you may like to subscribe to my RSS feed, or see other articles in the PHP category.
January 14th, 2008 at 21:30
Thanks for this Kieran my son! I had shyed away from MaxMind - perhaps assuming because they had a paid product their free one would be wizzle - but alas i was wrong.
That donate a beer button is awesome haha
January 14th, 2008 at 21:31
Does maxmind not have uk postcode info you think?