GEOIP and getting a computers IP address Location?

2019-01-24 14:38发布

I want my site to be able to locate a computers location, so if someone is accessing my site from london or manchester etc and to have users from a certain area displayed depending on their computers location. kind of like an online dating site that suggests users in your area.

I have been looking at this GEOIP database that lists all the cities around the world. But i dont know what to do next? Do i need to look at get ip address scripts that pull and compare the information from the GEOip database?

Please can someone point me in the right direction. Thanks.

GeoIP database from: http://dev.maxmind.com/geoip/geolite

标签: php ip
7条回答
【Aperson】
2楼-- · 2019-01-24 15:22

It depends on your need. I would suggest you use a web service so you do not need to worry about hosting the database and maintaining it on your own. All you need to do is parse the IP address of your visitor and get the result instantly. I am using the IP2location web service from this site.

查看更多
Anthone
3楼-- · 2019-01-24 15:24

I've used this one in PHP on my site. Very quick and easy to digest (xml etc): http://www.geoplugin.com/

查看更多
祖国的老花朵
4楼-- · 2019-01-24 15:29

Please note, this database is: In our recent tests, the GeoIP databases tested at 99.8% accurate on a country level, 90% accurate on a state level in the US, and 83% accurate for cities in the US within a 40 kilometer radius.

I would look into something like the: PEAR GeoLite library

A quick snippet of using this library in php:

$geoip = Net_GeoIP::getInstance(dirname(__FILE__) . '/data/GeoLiteCity.dat');
$ipaddress = '72.30.2.43'; // Yahoo!
$location = $geoip->lookupLocation($ipaddress);
var_dump($location);

The output will show, something similiar to:

object(Net_GeoIP_Location)[2]
    protected 'aData' =>
        array
        'countryCode' => string 'US' (length=2)
        'countryCode3' => string 'USA' (length=3)
        'countryName' => string 'United States' (length=13)
        'region' => string 'CA' (length=2)
        'city' => string 'Sunnyvale' (length=9)
        'postalCode' => string '94089' (length=5)
        'latitude' => float 37.4249
        'longitude' => float -122.0074
        'areaCode' => int 408
        'dmaCode' => float 807
查看更多
看我几分像从前
5楼-- · 2019-01-24 15:33

You probably already have GEOIP installed as a PECL extension. If you don't, then you can just install it yourself.

If you need anything more specific than country, you'll have to pay to get the DB, but the functions are still there.

查看更多
smile是对你的礼貌
6楼-- · 2019-01-24 15:40

Try the below code.

$ip =  $_SERVER['REMOTE_ADDR'];
echo $location = file_get_contents("http://api.hostip.info/country.php?ip=$ip");

Dont try this in your localhost. It will give the ip 127.0.0.1.

echo $location= file_get_contents("http://api.hostip.info/country.php?ip=12.215.42.19");
//outputs US
查看更多
Juvenile、少年°
7楼-- · 2019-01-24 15:41

You can use the services of https://geoip-db.com. A php JSONP example that returns the location of a given IP address.

<?php

    $json = file_get_contents('https://geoip-db.com/jsonp/82.196.6.158');
    $data = jsonp_decode($json);

    print $data->country_code . '<br>';
    print $data->country_name . '<br>';
    print $data->state . '<br>';
    print $data->city . '<br>';
    print $data->postal . '<br>';
    print $data->latitude . '<br>';
    print $data->longitude . '<br>';
    print $data->IPv4 . '<br>';

    function jsonp_decode($jsonp, $assoc = false) {
        if($jsonp[0] !== '[' && $jsonp[0] !== '{') {
            $jsonp = substr($jsonp, strpos($jsonp, '('));
        }
        return json_decode(trim($jsonp,'();'), $assoc);
    }
?>
查看更多
登录 后发表回答