[WordPress] 外掛分享: IP-to-Country

首頁外掛目錄 › IP-to-Country
20+
安裝啟用
★★★☆☆
3/5 分(1 則評價)
4287 天前
最後更新
問題解決
WordPress 2.8.0+ v0.08 上架:2010-03-27

內容簡介

提供用於查找 IP 地址所屬國家的 IP-to-Country WordPress 外掛,並不會向使用者提供任何輸出,但其他外掛作者可以使用此外掛快速且輕鬆地查找 IP 地址所屬國家。該外掛使用從 http://ip-to-country.webhosting.info(IPv4 only) 或 http://software77.net/geo-ip/(支援 IPv4 和 IPv6)等數據文件。只要它們具有相同格式:CSV 文件,採用雙引號 "" 標記字段,並使用逗號分隔項目、以 LF (\n) 結尾的行數,即可使用其他數據源。文件包含五列:

- IP 範圍的下界。
- IP 範圍的上界。兩者都表示為一個 32 位整數,可以使用 ip2long() 函數獲取。
- 2 個字母的國家代碼。
- 3 個字母的國家代碼。
- 國家名稱。

注意:為了匯入 IPv6 地址,必須先匯入 IPv4。原因是,IPv6 數據庫不携帶完整的國家信息,而 IPv4 數據庫則携帶。

操作如下:

- 要檢查是否安裝了 IP-to-Country 外掛:$ip2c_available = class_exists('PepakIpToCountry');
- 要查找有關數字 IP 地址的信息:

$country = PepakIpToCountry::IP_to_Country_Full('8.8.8.8'); // 返回 "UNITED STATES"
$country = PepakIpToCountry::IP_to_Country('8.8.8.8');
// 返回 "US"
$country = PepakIpToCountry::IP_to_Country_XX('8.8.8.8'); // 返回 "US"
$country = PepakIpToCountry::IP_to_Country_XXX('8.8.8.8'); // 返回 "USA"
$country = PepakIpToCountry::IP_to_Country_Info('8.8.8.8'); // 返回 Array('iso_name'=>'UNITED STATES', 'iso_code2'=>'US', 'iso_code3'=>'USA')
$image = PepakIpToCountry::IP_to_Country_Flag('8.8.8.8');

您可以將 IP 地址留空,此時將使用當前活躍用戶的 IP 地址。請注意:所有請求都被緩存,因此在讀取 IP_to_Country_Info 和使用 Full、XX 和 XXX 函数各自依次讀取三個信息之間,不應該有明顯的區別。

- 要在 SQL 中將您的插件表和 IP-to-Country 插件表進行接口:

提供了一個 "Subselect" 函數:
$subselect = PepakIpToCountry::Subselect($ip, $countryfield);

其中:

- $ip 是一個包含 IP 地址的字段,形式為一個 32 位數字。
- 如果您以四個用點分隔的數字('127.0.0.1')的方式儲存 IP 地址,可以使用 MySQL 的 INET_ATON 函數。
- 建議使用限定字 (加上表名,用點號分隔):$ip = 'INET_ATON(banlist.ip_address)';
- $countryfield 為您想要返回的字段之一:

- iso_name ─ 返回與PepakIpToCountry:: IP_to_Country_Full(...)相同的字符串。
- iso_code2 ─ 返回與PepakIpToCountry:: IP_to_Country_XX(...)相同的字符串。
- iso_code3 ─ 返回與PepakIpToCountry:: IP_to_Country_XX(...)相同的字符串。

注意:不進行檢查或驗證。

外掛標籤

開發者團隊

⬇ 下載最新版 (v0.08) 或搜尋安裝

① 下載 ZIP → 後台「外掛 › 安裝外掛 › 上傳外掛」
② 後台搜尋「IP-to-Country」→ 直接安裝(推薦)
📦 歷史版本下載

原文外掛簡介

IP-to-Country is a plugin which doesn’t provide any output to users, but which
may be used by plugin authors to quickly and easily find, in which country an
IP address is located.
To do so the plugin uses data files from either http://ip-to-country.webhosting.info
(IPv4 only) or http://software77.net/geo-ip/ (both IPv4 and IPv6).
Other sources are possible as long as they have the same format: CSV file,
fields enclosed with quotes (“) and separated with comma (,), lines ending
with LF (\n). Five columns:

Lower bound of an IP range.
Upper bound of an IP range. Both are represented as one 32-bit integer number,
such as you can get by using funtion ip2long().
2-character country code
3-character country code
Country name

Note: In order to import IPv6 addresses, IPv4 must be imported first. The
reason is, IPv6 database does not carry the full country information with
it, while the IPv4 database does.
Operations:

To detect if IP-to-Country plugin is installed:
$ip2c_available = class_exists(‘PepakIpToCountry’);

To find information about a numeric IP address:
$country = PepakIpToCountry::IP_to_Country_Full(‘8.8.8.8’);
// returns ‘UNITED STATES’
$country = PepakIpToCountry::IP_to_Country(‘8.8.8.8’);
$country = PepakIpToCountry::IP_to_Country_XX(‘8.8.8.8’);
// both return ‘US’
$country = PepakIpToCountry::IP_to_Country_XXX(‘8.8.8.8’);
// returns ‘USA’
$country = PepakIpToCountry::IP_to_Country_Info(‘8.8.8.8’);
// returns Array(‘iso_name’=>’UNITED STATES’, ‘iso_code2’=>’US’, ‘iso_code3’=>’USA’)
$image = PepakIpToCountry::IP_to_Country_Flag(‘8.8.8.8’);
// returns ”
You may leave the IP address empty; in that case, IP address of currently
active user is used instead.
Note: All requests are cached, so there shouldn’t be a noticeable difference
between reading IP_to_Country_Info and reading all three pieces of information
separately using the _Full, _XX and _XXX functions.

To interface your plugin’s tables to IP-to-Country plugin’s tables within SQL,
a function Subselect is published:
$subselect = PepakIpToCountry::Subselect($ip, $countryfield);

$ip is a field that contains IP address in the form of one 32bit number, e.g.

If you store IP addresses in the usual form of four dot-separated
numbers (‘127.0.0.1’), you can use MySQL’s function INET_ATON:
$ip = ‘INET_ATON(my_ip_address_field)’;
It is strongly recommended to use qualified field names (with tablename in
front, separated by a dot):
$ip = ‘INET_ATON(banlist.ip_address)’;

$countryfield is the field you want returned. It can be one of the following:
iso_name – returns the same string as PepakIpToCountry::IP_to_Country_Full(…)
iso_code2 – returns the same string as PepakIpToCountry::IP_to_Country_XX(…)
iso_code3 – returns the same string as PepakIpToCountry::IP_to_Country_XX(…)

Note: No checking or validation is done on either of these parameters. It is assumed
they are field names, NOT strings. DO NOT EVER USE VALUES THAT YOUR USERS CAN
INFLUENCE! This is important! If you let users supply arguments to Subselect(), you
are letting them to do whatever they like with your database!
This function only works with IPv4 addresses.
Example usage:
if (class_exists(‘PepakIpToCountry’))
$field = PepakIpToCountry(“INET_ATON(b.ip_addr)”, ‘iso_code2’);
else
$field = ‘NULL’;
$sql = “SELECT b.*, ${field} country_code FROM ${wpdb->prefix}banlist b WHERE …”;

Note: This plugin requires PHP5. If you want it to work with ZIP files
(e.g. for updating IP-country info from the default location), your PHP
must have extension php_zip enabled.

延伸相關外掛

文章
Filter
Apply Filters
Mastodon