简体   繁体   English

如何在PHP中使用MaxMind GeoIP?

[英]How to use MaxMind GeoIP in PHP?

I am using the binary version of MaxMind's GEOIPCITY database. 我正在使用MaxMind的GEOIPCITY数据库的二进制版本。 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. 除了网络速度,ISP等,我真的不需要任何其他东西。

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? 或者我是否必须处理mysql版本中的所有内容?

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. 我更喜欢只使用二进制版本,因为它更快,但现在我不确定是否可以在不使用mysql版本的情况下显示某个城市下的所有拉链或某个国家/地区下的所有州等。 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. GeoIP数据库仅用于一个目的,仅用于一个目的:返回输入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. 不是通用的GIS数据库,并且不会用于此目的,因为有些位置不会为任何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 Maxmind有一个单独的“人口世界城市”数据库,更有可能适用于此目的: http//www.maxmind.com/app/worldcities

I just wrote a linux daemon to serve MaxMind city-level geoip queries. 我刚刚写了一个linux守护进程来提供MaxMind城市级geoip查询。 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). 每个查询大约需要3微秒,当它运行时,服务器在64位机器上占用大约550MB的RAM(在32位机器上占用300MB)。

https://github.com/homer6/geoipd 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. 来自Maxmind的二进制数据库可能不是为此目的而设计的。 Try downloading their .csv file, export it into database and you can then index locations table on 'state' field. 尝试下载他们的.csv文件,将其导出到数据库中,然后您可以在“州”字段上索引位置表。 Then can easily select all cities from one state. 然后可以轻松地从一个州选择所有城市。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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