[精讚] [會員登入]
3041

[Centos 6& 7] 安裝php-geoip

php-geoip是PHP由domain、ip查詢城市國家資料非常強的函式

分享此文連結 //n.sfs.tw/10399

分享連結 [Centos 6& 7] 安裝php-geoip@新精讚
(文章歡迎轉載,務必尊重版權註明連結來源)
2024-06-08 01:35:40 最後編修
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+PHP5安裝

# 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

docke中安裝

Dockerfile加入

RUN apt-get install -y geoip-database libgeoip-dev && \ pecl install geoip-beta && \ docker-php-ext-enable geoip

 

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

 

END

你可能感興趣的文章

[PHP8] 使用autoload autoload+ namespace +use 到了php7之後,namespace和use越來越重要,此篇整理autoload和namespace、use的結合使用。

Smarty安裝 smarty 是著名的樣版引擎,非常的好用,用多了突然發現拿掉smarty反而不會寫php了,以下是安裝過程..

[PHP] UTF8中取出字串中特定的字數 要將字串,例如資料庫取出的TEXT,取出特定的字數

[CodeIgniter 3] 資料庫的使用方法整理2/2 CI3 承襲 CI2,有很多的builder class可以用,依各人的使用習慣,有人喜歡一堆sql字串,有人喜歡用helper

[PHP] 檢查IP是否在某個網段內 mtachcidr 要檢查IP是否在某個網段內,要寫幾行?10行?5行? 不用,只要2行。以下是我寫的 code /** * matchCI

[Smarty5] 如何在樣版中使用php本身的函式 解決 smarty5版後不能直接叫用 php函式的問題

隨機好文

如何在linux下執行java 原生的java應用程式可以使用簡單的方法在console下面寫出來,適合作簡單的應用

為什麼要重造輪子? 什麼輪子?造什麼輪子?我為什麼要重造輪子?

[Win7] 燒錄 iso 檔 在Windows7 中內建燒錄程式,可以直接把檔案拉到光碟機裡,再執行燒錄。

維修冰箱 維修冰箱

[Wildfly10] 發佈war檔 deploy war file onto wildfly10