How to get the Country according to a certain IP?

2019-01-03 22:21发布

Does anyone know of a simple way to retrieve the country for a given IP Address? Preferably in ISO_3166-1 format?

14条回答
ら.Afraid
2楼-- · 2019-01-03 22:54

Here's a nice free service with a public API: http://www.hostip.info/use.html

查看更多
小情绪 Triste *
3楼-- · 2019-01-03 22:54
forever°为你锁心
4楼-- · 2019-01-03 22:56

you can use web service API's which do this work like:

see example of service: http://ip-api.com and usage: http://whatmyip.info
查看更多
男人必须洒脱
5楼-- · 2019-01-03 22:59

Try this php code

  <?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];?>
查看更多
乱世女痞
6楼-- · 2019-01-03 22:59

You can try the free IP2Location LITE database

To create the table in 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;

To import the data

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 code to query the 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);
 }
}
?>
查看更多
做自己的国王
7楼-- · 2019-01-03 23:06

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;
查看更多
登录 后发表回答