Jak uzyskać kraj według określonego IP?

Czy ktoś zna prosty sposób na odzyskanie kraju dla danego adresu IP? Najlepiej w formacie ISO_3166-1?

Author: Paul Ratazzi, 2008-08-04

14 answers

Wiele osób (w tym moja firma) zdaje się używać MaxMind GeoIP.

Mają darmową wersję GeoLite , która nie jest tak dokładna jak wersja płatna, ale jeśli szukasz czegoś prostego, może to być wystarczająco dobre.

 36
Author: Orion Edwards,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-10-08 00:17:40

Istnieją dwa podejścia: korzystanie z usługi internetowej i korzystanie z jakiejś lokalnej listy (być może zawiniętej w bibliotekę). To, czego chcesz, zależy od tego, co budujesz.

Dla usług:

Dla list:

 38
Author: Warren Blanchet,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:26:00

Oto fajna darmowa usługa z publicznym API: http://www.hostip.info/use.html

 10
Author: Mark Harrison,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-08-04 05:21:59

Ipinfodb zapewnia darmową bazę danych i API dla IP do kraju i odwrotnie. Używają darmowych danych z MaxMind. Dane są aktualizowane co miesiąc i jest to świetna darmowa alternatywa z przyzwoitą dokładnością.

 8
Author: Donny Kurnia,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-25 13:54:58

Nie wiem jak dokładne to hostip.info strona jest. Właśnie odwiedziłem tę stronę i zgłosiłem, że moim krajem jest Kanada. Jestem w USA, a dostawca usług internetowych, z którego korzysta moje biuro, działa tylko z USA. Pozwala to na poprawienie domyślenia, ale jeśli korzystasz z tej usługi do śledzenia vistors witryn internetowych według kraju, nie będziesz miał możliwości sprawdzenia, czy dane są poprawne. Oczywiście, jestem tylko jednym punktem danych. Pobrałem bazę danych GeoLite Country, która jest tylko ... plik csv i mój adres IP został prawidłowo zidentyfikowany jako my.

Kolejną zaletą linii produktów MaxMind (płatnej lub bezpłatnej) jest to, że masz dane, nie ponosisz hitu wydajności wywołania usługi internetowej do innego systemu.

 5
Author: Chris Miller,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-08-04 14:02:14

Najdokładniejsze są elementy cyfrowe NetAcuity...nie za darmo, ale dostajesz to, za co płacisz przez większość czasu....Element Cyfrowy

 2
Author: CSharpAtl,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-09-27 03:04:41

Google ' s clientlocation returns ( my example )

latlng = new google.maps.LatLng(google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude);
location = "IP location: " + getFormattedLocation();
document.getElementById("location").innerHTML = location;
 2
Author: Niklas Rosencrantz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-01-13 13:25:21

Możesz użyć rozwiązania podanego dla tego pytania .

Ale zwraca dwucyfrowy kod kraju.

 2
Author: zak,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:02:20

Wypróbuj ten kod 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];?>
 2
Author: pckabeer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-07-30 08:07:35

Możesz skorzystać z moich usług, http://ipinfo.io , za to. API zwraca całą masę różnych szczegółów dotyczących adresu 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
}

Jeśli szukasz tylko kodu kraju, wystarczy dodać / country do adresu URL:

$ curl ipinfo.io/8.8.8.8/country
US

Oto ogólna funkcja PHP, której możesz użyć:

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

Użyłem IP 8.8.8.8 w tych przykładach, ale jeśli chcesz podać szczegóły dotyczące IP użytkownika, po prostu podaj $_SERVER['REMOTE_ADDR']. Więcej szczegółów na stronie http://ipinfo.io/developers

 2
Author: Ben Dowling,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-08-26 04:49:49
 1
Author: saifur,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-06-27 17:54:01

Możesz użyć API usług internetowych, które wykonują taką pracę jak:

see example of service: http://ip-api.com and usage: http://whatmyip.info
 1
Author: user3463375,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-05-28 12:57:32

Zobacz ipdata.co który daje kilka punktów danych z adresu ip.

API jest dość szybkie, z 10 globalnymi punktami końcowymi, z których każdy jest w stanie obsłużyć >800m połączeń dziennie.

Oto przykład curl;

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
    }
}⏎  
 0
Author: Jonathan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-18 19:05:11

Możesz wypróbować darmową bazę danych IP2Location LITE

Aby utworzyć tabelę w 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;

Aby zaimportować dane

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;

Kod PHP do zapytania 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);
 }
}
?>
 0
Author: Vlam,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-27 05:32:26