[Centos 6& 7] 安裝php-geoip

URL Link //n.sfs.tw/10399

2016-12-08 14:48:26 By 張○○

函式庫說明

http://php.net/manual/en/book.geoip.php

檢查

$ php -m | grep 'geoip'
<空的結果代表沒裝,預設沒有,如果有裝會顯示'geoip'>

安裝

Centos6安裝

# php -v
PHP 5.5.38 (cli) (built: Sep 19 2016 13:55:55)

# yum install php-geoip

Centos7安裝

# php -v
PHP 5.6.28 (cli) (built: Nov 10 2016 20:36:59)

# yum install php56w-pecl-geoip

如果你的PHP版本是5.5
# yum install php55w-pecl-geoip

安裝完後重啟apache
# apachectl restart

Centos7+PHP7安裝

# php -v
PHP 7.0.18 (cli) (built: Apr 15 2017 07:09:11) ( NTS )

# yum install php70w-devel gcc GeoIP-devel  php70w-pear

# pecl install geoip-1.1.1

安裝完後在 php extension 目錄(可能在 /usr/lib64/php/modules)會有這個檔 geoip.so

# cd /etc/php.d

# vi geoip.ini

加上一行存檔離開重啟httpd

extension=geoip.so

PHP

<?php
print geoip_country_code3_by_name ('tw.yahoo.com' ); // TWN
print geoip_country_code_by_name ('123.45.67.89' ); //KR

使用自帶的地理資料庫

MAXMIND 公司有提供地理資料庫GeoLite Legacy Downloadable Databases下載,包含有IPv4及IPv6的資料

公司 https://www.maxmind.com/en/

下載頁面 http://dev.maxmind.com/geoip/legacy/geolite/#IP_Geolocation

可以自行下載後用使用該公司的資料,但在授權使用上必須把該公司連結放在頁面上。

安裝

# wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz

# gunzip GeoLiteCity.dat.gz

# mkdir -v /usr/share/GeoIP

# mv -v GeoLiteCity.dat /usr/share/GeoIP/GeoIPCity.dat

PHP

geoip_setup_custom_directory('/usr/share/GeoIP/'); //指定資料庫目錄
print_r(geoip_database_info());  //資料庫來源、版號及版權
print_r(geoip_db_get_all_info());  //資料庫內容
print geoip_country_code3_by_name ('tw.yahoo.com' ); // TWN
print geoip_country_code_by_name ('123.45.67.89' ); //KR