Need: Geo-IP - Show city name by IP script

Status
Not open for further replies.

hydrogin

New member
Jun 24, 2006
274
4
0
Like the title says - if you guys know of a good snippet/script - please let me know

thanks
 


Go here and download the files: MaxMind - GeoLite City | Free Geolocation Database

Then use this code to get the city name.

include("geoipcity.inc");
include("geoipregionvars.php");
$gi = geoip_open("/fullpath/to/GeoIP/GeoLiteCity.dat",GEOIP_STANDARD);
$ip = $_SERVER['REMOTE_ADDR'];
$record = geoip_record_by_addr($gi,$ip);
$city = $record->city;
 
This also works...

Code:
<script src="http://www.google.com/jsapi" type="text/javascript"></script>

<script type="text/javascript"><!--
if (typeof(google.loader.ClientLocation.address.city) != null) {
  document.write(google.loader.ClientLocation.address.city
  +", "
  +google.loader.ClientLocation.address.region);
} else {
  document.write("Walnut Creek, CA")
}
// --></script>
 
  • Like
Reactions: tvmatt
$gi = geoip_open("/fullpath/to/GeoIP/GeoLiteCity.dat",GEOIP_STANDARD);

You should use the GEOIP_MEMORY_CACHE or GEOIP_SHARED_MEMORY_CACHE (depending on which one works, latter is preferred) to save it reading the file on each request, as it takes a lot more time and resources.
 
Status
Not open for further replies.