4

我希望我的站点能够找到计算机位置,因此如果有人从伦敦或曼彻斯特等地访问我的站点,并根据他们的计算机位置显示来自某个区域的用户。有点像一个在线约会网站,向您所在地区的用户提供建议。

我一直在查看这个列出全球所有城市的 GEOIP 数据库。但我不知道下一步该怎么做?我是否需要查看从 GEOip 数据库中提取和比较信息的获取 IP 地址脚本?

请有人指出我正确的方向。谢谢。

GeoIP 数据库来自: http ://dev.maxmind.com/geoip/geolite

4

7 回答 7

4

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.

于 2012-11-02T09:37:53.607 回答
4

PHP 具有 GeoIP 的内置函数

GeoIP 库

这个函数可能特别有用,因为它将以数组的形式返回大陆、国家、城市等。

geoip_record_by_name

用作$_SERVER['REMOTE_ADDR']您的 ip。

$record = geoip_record_by_name( $_SERVER['REMOTE_ADDR']);

于 2012-10-31T15:39:37.470 回答
4

试试下面的代码。

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

不要在您的本地主机中尝试此操作。它将提供 ip 127.0.0.1。

echo $location= file_get_contents("http://api.hostip.info/country.php?ip=12.215.42.19");
//outputs US
于 2012-10-31T15:38:47.960 回答
1

我在我的网站上的 PHP 中使用了这个。非常快速且易于消化(xml 等):http ://www.geoplugin.com/

于 2012-10-31T15:40:53.223 回答
1

请注意,这个数据库是: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.

我会研究类似的东西:PEAR GeoLite library

在 php 中使用这个库的快速片段:

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

输出将显示类似于:

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
于 2012-10-31T15:41:41.543 回答
0

您可能已经将GEOIP作为 PECL 扩展安装。如果你不这样做,那么你可以自己安装它。

如果您需要比国家更具体的内容,则必须付费才能获得数据库,但功能仍然存在。

于 2012-10-31T15:40:01.437 回答
0

您可以使用https://geoip-db.com的服务。返回给定 IP 地址位置的 php JSONP 示例。

<?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);
    }
?>
于 2019-01-09T13:49:37.287 回答