[精讚] [會員登入]
1893

[Centos 6& 7] 安裝php-geoip

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

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

分享連結 [Centos 6& 7] 安裝php-geoip@新精讚
(文章歡迎轉載,務必尊重版權註明連結來源)
2019-10-25 09:49:36 最後編修
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

 

END

你可能感興趣的文章

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

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

使用Yahoo OAuth2 2/2 使用Yahoo OAuth2認證我的網頁

[phpmyadmin] 設定預設語言(Language) phpmyadmin預設進來是會選擇你瀏覽器預設的語系,改成正體中文

[PHP] CodeIgniter 3+pure+smarty安裝及環境設置2/2 PHP framework CodeIgniter 3+ pure CSS +smarty Template Engine的整合#2

PHP 數字加解密函式 自寫的數字加解密,勉強用

我有話要說

>>

限制:留言最高字數1000字。 限制:未登入訪客,每則留言間隔需超過10分鐘,每日最多5則留言。

訪客留言

[無留言]

隨機好文

UTF-8 BOM (Byte Order Mark) 的問題 在 Michael Kaplan 那看到 Every character has a story #4: U+feff

維修冰箱 維修冰箱

詭異的創業思維 創業的思維中,有多少銀彈,有多少技術,有多少人脈,有多少時間等等,每個都要考慮進去,以熱忱建立的關係脆弱的像蘇打餅乾一樣..

[AS3] 變數型態 基本類型宣告 as3 有下列幾種基本類型:string, int, number, object, boolean, n

[Mysql] 資料型態int, float, double, text, char, varchar, blob大小 Mysql中資料型態int, float, text, char, varchar, blob大小