[精讚] [會員登入]
3053

[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

你可能感興趣的文章

使用strcmp來判斷日期是否介於某日期之間 要比較現在日期是否在兩個日期之間,使用字串比對的方法

[PHP] 使用FTP PHP 上使用 FTP 的寫法

[PHP] 判斷文字、數字、文字加數字的方法 幾個PHP數字和文字操作上的小眉角:判斷文字、數字、文字加數字的方法

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

PHP 產生連續的日期 要用PHP產生連續的日期,連續月份

[PHP] 字串編碼及解碼函式 為何要將字串編碼?理由很簡單,就是不要讓人家輕易的知道字串內容是什麼。例如點選分頁時,我們常會用這樣的連結: index

隨機好文

[MAC] 截取螢幕畫面的方法 截取螢幕畫面的方法,在MAC中叫作螢幕快照,英文是screenshot

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

精讚的版面變化 ▓此文僅作為舊文的記錄▓ 這篇文章為了紀念改版完成而撰寫。 原本的部落格是民國97年的作品,那時還是用舊有的技術來寫,很

問問題 問問題其實內涵很深,我悟了很久才懂。 有人問題的目的並不一定是想要得到答案,有時只是純粹想問問題..

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