简体   繁体   中英

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 = 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?

First of all, gethostbyname won't give you a country. You have to resort to geolocation for that. A free service which works OK is http://freegeoip.net . They provide a API:

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.


The best column type to store an IP address is INT(10) unsigned , which will take up less space than a varchar column. Conversion is easily handled with INET_ATON and INET_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.

Alternatively, there is a nice library for this task: http://geocoder-php.org/

Identifiying a user's location by their IP is called Geolocation . It usually requires a database that maps IP address ranges to geographic locations.

The Maxmind company provides such a database called GeoLite for free, under a CreativeCommons license

GeoLite databases are licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. 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. 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.

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#)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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