题
任何人都不会知道的简单的方式检索该国的IP地址吗?最好在ISO_3166-1的格式?
解决方案
很多人(包括我的公司)似乎使用我们GeoIP.
他们有免费版本 GeoLite 这是不准确的付费版本,但是如果你只是后简单的东西,可能不够好。
其他提示
有两个办法:使用互联网服务和使用某些种类的地的名单(也许包裹在一个图书馆)。你想要什么将取决于你是什么建筑。
对于服务:
用于清单:
- http://www.maxmind.com/app/geoip_country (如通过 Orion)
你可以滚你自己通过下载列出了从该地址:
这是一个很好的免费服务与公共API:http://www.hostip.info/use.html
ipinfodb 提供免费的数据库和API for IP国家,反之亦然。他们使用的免费数据从我们.得到的数据每月更新一次,这是一个伟大的自由选择与体面的准确性。
我不知道如何准确的,hostip.info 网站。我只是visted该网站,它报告说,我国是加拿大。我在美国的ISP,我的办公室只使用开美国。它不会让你来纠正它们的猜测,但是如果您使用这项服务,以跟踪网站访客的国家,只有没有办法知道如果数据是正确的。当然,我只是一个数据点。我下载了GeoLite国家数据库,该数据库只需一个.csv文件,我的IP地址是正确识别作为我们的。
另一个好处的我们的产品线(付费或免费)是的,你有数据,就不会产生的绩效打击,使一个网络服务调到另一个系统。
最精确的数字要素NetAcuity...不是免费的,但你得到你所支付的大部分时间。数字元
你可以使用溶液的提供 这个问题.
但它返回一个2位数的国家代码。
试试这个php码
<?php $ip = $_SERVER['REMOTE_ADDR'];
$json = file_get_contents("http://api.easyjquery.com/ips/?ip=".$ip."&full=true");
$json = json_decode($json,true);
$timezone = $json[localTimeZone];?>
你可以用我的服务, http://ipinfo.io, 为此。API返回一大堆不同的详细信息的一个IP地址:
$ curl ipinfo.io/8.8.8.8
{
"ip": "8.8.8.8",
"hostname": "google-public-dns-a.google.com",
"loc": "37.385999999999996,-122.0838",
"org": "AS15169 Google Inc.",
"city": "Mountain View",
"region": "CA",
"country": "US",
"phone": 650
}
如果你是只有在国家代码你只需要增加国家/的网址:
$ curl ipinfo.io/8.8.8.8/country
US
这是一个通用的PHP function你可以使用:
function ip_details($ip) {
$json = file_get_contents("http://ipinfo.io/{$ip}");
$details = json_decode($json);
return $details;
}
$details = ip_details("8.8.8.8");
echo $details->city; // => Mountain View
echo $details->country; // => US
echo $details->org; // => AS15169 Google Inc.
echo $details->hostname; // => google-public-dns-a.google.com
我已经使用的IP8.8.8.8在这些例子,但是如果你想要的详细信息的用户的知识产权只有通过在 $_SERVER['REMOTE_ADDR']
代替。更多详情可在 http://ipinfo.io/developers
使用该功能ipToCountry($ip) http://www.mmtutorialvault.com/php-ip-to-country-function/
你可以使用网络服务API其做这项工作,如:
see example of service: http://ip-api.com and usage: http://whatmyip.info
看看 ipdata.同 这给你几个数据点的ip地址。
API是相当快的,有10个全球性的端点的每一个能够处理>800米的电话日。
这里是卷曲的例子;
curl https://api.ipdata.co/78.8.53.5
{
"ip": "78.8.53.5",
"city": "G\u0142og\u00f3w",
"region": "Lower Silesia",
"region_code": "DS",
"country_name": "Poland",
"country_code": "PL",
"continent_name": "Europe",
"continent_code": "EU",
"latitude": 51.6461,
"longitude": 16.1678,
"asn": "AS12741",
"organisation": "Netia SA",
"postal": "67-200",
"currency": "PLN",
"currency_symbol": "z\u0142",
"calling_code": "48",
"flag": "https://ipdata.co/flags/pl.png",
"emoji_flag": "\ud83c\uddf5\ud83c\uddf1",
"time_zone": "Europe/Warsaw",
"is_eu": true,
"suspicious_factors": {
"is_tor": false
}
}⏎
你可以尝试自由 IP2Location精简数据库
创建表在MySQL
CREATE DATABASE ip2location;
USE ip2location;
CREATE TABLE `ip2location_db1`(
`ip_from` INT(10) UNSIGNED,
`ip_to` INT(10) UNSIGNED,
`country_code` CHAR(2),
`country_name` VARCHAR(64),
INDEX `idx_ip_to` (`ip_to`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
对进口数据
LOAD DATA LOCAL
INFILE 'IP2LOCATION-LITE-DB1.CSV'
INTO TABLE
`ip2location_db1`
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 0 LINES;
PHP代码查询MySQL
<?php
// Replace this MYSQL server variables with actual configuration
$mysql_server = "mysql_server.com";
$mysql_user_name = "UserName";
$mysql_user_pass = "Password";
// Retrieve visitor IP address from server variable REMOTE_ADDR
$ipaddress = $_SERVER["REMOTE_ADDR"];
// Convert IP address to IP number for querying database
$ipno = Dot2LongIP($ipaddress);
// Connect to the database server
$link = mysql_connect($mysql_server, $mysql_user_name, $mysql_user_pass) or die("Could not connect to MySQL database");
// Connect to the IP2Location database
mysql_select_db("ip2location") or die("Could not select database");
// SQL query string to match the recordset that the IP number fall between the valid range
$query = "SELECT * FROM ip2location_db1 WHERE $ipno <= ip_to LIMIT 1";
// Execute SQL query
$result = mysql_query($query) or die("IP2Location Query Failed");
// Retrieve the recordset (only one)
$row = mysql_fetch_object($result);
// Keep the country information into two different variables
$country_code = $row->country_code;
$country_name = $row->country_name;
echo "Country_code: " . $country_code . "<br/>";
echo "Country_name: " . $country_name . "<br />";
// Free recordset and close database connection
mysql_free_result($result);
mysql_close($link);
// Function to convert IP address (xxx.xxx.xxx.xxx) to IP number (0 to 256^4-1)
function Dot2LongIP ($IPaddr) {
if ($IPaddr == "")
{
return 0;
} else {
$ips = explode(".", $IPaddr);
return ($ips[3] + $ips[2] * 256 + $ips[1] * 256 * 256 + $ips[0] * 256 * 256 * 256);
}
}
?>