php geoip

1.命令方式安装安装GeoIP

apt-get install php7.3-geoip php7.3-dev libgeoip-dev

GeoIP安装完成后,IP库文件会安装在这个目录下:/usr/share/GeoIP
2.编译安装geoip扩展

首先要下载geoip扩展源码,最新版地址:https://pecl.php.net/package/geoip。
编译成动态库:
$ wget https://pecl.php.net/get/geoip-1.1.1.tgz
$ cd geoip-1.1.1
$ phpize
$ ./configure
$ make
$ sudo make install

安装完成后,在/usr/lib64/php/modules/目录下(64位系统)生成geoip.so链接库。
打开php.ini文件,在最后加上:
extension=geoip.so
重启PHP-FPM就可以用了。
如果不想安装扩展,也可以直接用PHP代码加载IP库:https://github.com/maxmind/geoip-api-php
3.PHP中直接使用

详细用法请参考PHP官网:http://php.net/manual/zh/ref.geoip.php。
示例:
NA
[country_code] => US
[country_code3] => USA
[country_name] => United States
[region] => CA
[city] => Milpitas
[postal_code] => 95035
[latitude] => 37.440399169922
[longitude] => -121.87049865723
[dma_code] => 807
[area_code] => 408
)
更新IP库

GeoIP默认使用Maxmind提供的IP库,这可以用geoip_database_info()方法看到。

例如我的显示:

GEO-106FREE 20170404 Build 1 Copyright (c) 2017 MaxMind Inc All Rights Reserved

在Maxmind网站上,也提供了GeoIP库自动更新方法:http://dev.maxmind.com/geoip/geoipupdate/。

Maxmind提供的IP库会在每个月的第一个星期二更新一次,所以我们在此时间后一天执行更新就能拿到最新的APNIC的IP库了。

首先查看是否有文件:/etc/GeoIP.conf。

如果没有,就创建一个,内容:

# The following UserId and LicenseKey are required placeholders:
UserId 999999
LicenseKey 000000000000

# Include one or more of the following ProductIds:
# * GeoLite2-City - GeoLite 2 City
# * GeoLite2-Country - GeoLite2 Country
# * GeoLite-Legacy-IPv6-City - GeoLite Legacy IPv6 City
# * GeoLite-Legacy-IPv6-Country - GeoLite Legacy IPv6 Country
# * 506 - GeoLite Legacy Country
# * 517 - GeoLite Legacy ASN
# * 533 - GeoLite Legacy City
ProductIds GeoLite2-City GeoLite2-Country GeoLite-Legacy-IPv6-City GeoLite-Legacy-IPv6-Country 506 517 533

然后在Linux下手动执行一遍更新命令:
geoipupdate -v
如果没有出错,就完成更新GeoIP库了。

4.1 crontab自动更新IP库
我们可以使用Linux crontab可以实现自动更新IP库。
使用如下命令打开当前用户的crontab文件:
crontab -e
添加一行:
30 2 * * 3#1 /usr/bin/geoipupdate
这一段的功能是,在每个月第一个星期三自动执行 geoipupdate 命令,执行时间在凌晨2点30分。

因为Maxmind是每月的第一个星期二更新IP库,所以我们选择延迟一点,避免时差引起误差。
然后重启 crond:
/sbin/service crond restart
如果有防火墙,geoipupdate需要 DNS 和HTTPS(443)端口打开。

4.2 手动更新IP库
如果用PHP更新,让PHP执行SHELL命令执行更新:

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注