简体   繁体   English

按IP地址识别用户的位置

[英]Identifying user's location by IP address

I was writing a program where I wanted the country of the user to be automatically identified. 我正在编写一个程序,我希望自动识别用户的国家/地区。 I wrote code that takes the ip and stores it in the DB. 我编写了代码来获取ip并将其存储在DB中。

$ip = gethostbyname($_SERVER['REMOTE_ADDR']);

Storing: 储存:

 <?php `$result = mysql_query("INSERT INTO `wh_order` (`name`, `email`, `contact`, `itemid`, `itemquantity`, `ip`,`message`, `date`) VALUES('".$name."','".$email."','".$contact."','".$itemid."','".$itemquantity."','".$ip."','".$message."', NOW())");` ?>

Also, it this the best method of storing ip address? 另外,这是存储IP地址的最佳方法吗?

First of all, gethostbyname won't give you a country. 首先,gethostbyname不会给你一个国家。 You have to resort to geolocation for that. 你不得不求助于地理定位。 A free service which works OK is http://freegeoip.net . 一个有效的免费服务是http://freegeoip.net They provide a API: 他们提供API:

http://freegeoip.net/json/74.125.143.104 http://freegeoip.net/json/74.125.143.104

Which returns: 哪个回报:

{
    "city": "Mountain View",
    "region_code": "CA",
    "region_name": "California",
    "metrocode": "807",
    "zipcode": "94043",
    "longitude": "-122.057",
    "latitude": "37.4192",
    "country_code": "US",
    "ip": "74.125.227.136",
    "country_name": "United States"
}

Note that geolocation by IP wont be 100% accurate. 请注意,IP地理定位不会100%准确。


The best column type to store an IP address is INT(10) unsigned , which will take up less space than a varchar column. 存储IP地址的最佳列类型是INT(10) unsigned ,这将比varchar列占用更少的空间。 Conversion is easily handled with INET_ATON and INET_NTOA : 使用INET_ATONINET_NTOA可以轻松处理转换:

mysql> SELECT INET_ATON('10.0.5.9');
        -> 167773449

mysql> SELECT INET_NTOA(167773449);
        -> '10.0.5.9'

You could use the databases of maxmind (http://www.maxmind.com/app/country) in combination with an apache or nginx extension. 您可以将maxmind(http://www.maxmind.com/app/country)的数据库与apache或nginx扩展结合使用。

Alternatively, there is a nice library for this task: http://geocoder-php.org/ 或者,这个任务有一个很好的库: http//geocoder-php.org/

Identifiying a user's location by their IP is called Geolocation . 通过IP识别用户的位置称为地理位置 It usually requires a database that maps IP address ranges to geographic locations. 它通常需要一个将IP地址范围映射到地理位置的数据库。

The Maxmind company provides such a database called GeoLite for free, under a CreativeCommons license Maxmind公司根据CreativeCommons许可免费提供名为GeoLite的数据库

GeoLite databases are licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. GeoLite数据库根据知识共享署名 - 相同方式共享3.0 Unported许可进行许可。 Commercial licensing is available. 提供商业许可。

They have an apache module that allows for quick IP lookups, the easiest way to get started, however, is probably the pure PHP option. 他们有一个允许快速IP查找的apache模块,但最简单的入门方式可能是纯PHP选项。 Check out the list of possible integration methods. 查看可能的集成方法列表。

The download archive includes several example, the basic process is rather straightforward: 下载档案包括几个例子,基本过程相当简单:

$gi = geoip_open("GeoLite.dat", GEOIP_STANDARD);
$record = geoip_record_by_addr($gi, "24.24.24.24");
echo $record->latitude;
echo $record->longitude;

Use the services of https://geoip-db.com They support IPV4 and IPV6 addresses to locate an ip's geolocation. 使用https://geoip-db.com的服务他们支持IPV4和IPV6地址来定位ip的地理位置。

Example: 例:

<!DOCTYPE html>
<html>
<head>
    <title>GEOIP DB - Geolocation by IP</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js">
    </script>
</head>
<body>
    <div>Country: <span id="country"></span>
    <div>State: <span id="state"></span>
    <div>City: <span id="city"></span>
    <div>Latitude: <span id="latitude"></span>
    <div>Longitude: <span id="longitude"></span>
    <div>IP: <span id="ip"></span>
    <script>
        $.ajax({
            url: "https://geoip-db.com/jsonp",
            jsonpCallback: "callback",
            dataType: "jsonp",
            success: function( location ) {
                $('#country').html(location.country_name);
                $('#state').html(location.state);
                $('#city').html(location.city);
                $('#latitude').html(location.latitude);
                $('#longitude').html(location.longitude);
                $('#ip').html(location.IPv4);  
            }
        });     
    </script>
</body>
</html>

This is a jquery snippet, although other examples can be found on their website (Plain javascript, php, C#) 这是一个jquery片段,虽然其他例子可以在他们的网站上找到(普通的javascript,php,C#)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM