简体   繁体   中英

How to use MaxMind GeoIP in PHP?

I am using the binary version of MaxMind's GEOIPCITY database. The following code gives me all the information I need about my visitors:

include("geoipcity.inc");
include("geoipregionvars.php");

$gi = geoip_open("GeoLiteCity.dat", GEOIP_STANDARD);
print_r( geoip_db_get_all_info() );
$record = geoip_record_by_addr($gi, $user_ip);
print $record->country_code . " " . $record->country_code3 . " " . $record->country_name . "<br /><br />";
print $record->region . " " . $GEOIP_REGION_NAME[$record->country_code][$record->region] . "<br /><br />";
print $record->city . "<br /><br />";
print $record->postal_code . "<br /><br />";
print $record->latitude . "<br /><br />";
print $record->longitude . "<br /><br />";
print $record->metro_code . "<br /><br />";
print $record->area_code . "<br /><br />";
print $record->continent_code . "<br /><br />";
geoip_close($gi);

I don't really need anything beyond this like network speed, ISP, etc.

My issue is, there are parts of my site where I need to display all the cities under a particular state, or all the states/regions in a particular country. For example I need to do this in the registration form.

Is there no way to get these lists by querying the binary file? or do I have to handle all that from the mysql version?

I would prefer to use only the binary version since it's faster, but now I am not sure if the displaying of all zips under a certain city or all states under a certain country, etc. is possible without using the mysql version. Does one need to use both?

http://www.maxmind.com/app/php
http://geolite.maxmind.com/download/geoip/api/php/

The GeoIP database is intended for one purpose, and one purpose only: to return location information for an input IP. It is not a general-purpose GIS database, and will not work for that purpose, as there are some locations which won't be returned for any IP. (For instance, a small town with no local Internet service might not appear in their database at all, since any IP in that town would likely be categorized under another nearby city.)

Maxmind has a separate "World Cities with Population" database that is more likely to be suitable for this purpose: http://www.maxmind.com/app/worldcities

I just wrote a linux daemon to serve MaxMind city-level geoip queries. Each query takes about 3 microseconds and while it's running, the server takes up about 550MB of ram on a 64bit machine (and 300MB on a 32bit machine).

https://github.com/homer6/geoipd

I'm sure you can modify it to index states as well. Let me know if you need help and I'll gladly help you modify it.

Hope that helps...

Binary db from Maxmind is probably not designed for this purpose. Try downloading their .csv file, export it into database and you can then index locations table on 'state' field. Then can easily select all cities from one state.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM