获取用户的当前位置

发布于 2024-10-26 05:38:31 字数 38 浏览 4 评论 0原文

如何根据 IP 地址确定用户的当前位置(我猜它是这样工作的)。

How can I determine a user's current location based on IP address (I guess it works this way).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

美胚控场 2024-11-02 05:38:31
<?php
$user_ip = getenv('REMOTE_ADDR');
$geo = unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=$user_ip"));
$country = $geo["geoplugin_countryName"];
$city = $geo["geoplugin_city"];
?>
<?php
$user_ip = getenv('REMOTE_ADDR');
$geo = unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=$user_ip"));
$country = $geo["geoplugin_countryName"];
$city = $geo["geoplugin_city"];
?>
赠我空喜 2024-11-02 05:38:31

已编辑

freegeoip.net更改为ipinfo.io

<?php    

function get_client_ip()
{
    $ipaddress = '';
    if (isset($_SERVER['HTTP_CLIENT_IP'])) {
        $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
    } else if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else if (isset($_SERVER['HTTP_X_FORWARDED'])) {
        $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
    } else if (isset($_SERVER['HTTP_FORWARDED_FOR'])) {
        $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
    } else if (isset($_SERVER['HTTP_FORWARDED'])) {
        $ipaddress = $_SERVER['HTTP_FORWARDED'];
    } else if (isset($_SERVER['REMOTE_ADDR'])) {
        $ipaddress = $_SERVER['REMOTE_ADDR'];
    } else {
        $ipaddress = 'UNKNOWN';
    }

    return $ipaddress;
}
$PublicIP = get_client_ip();
$json     = file_get_contents("http://ipinfo.io/$PublicIP/geo");
$json     = json_decode($json, true);
$country  = $json['country'];
$region   = $json['region'];
$city     = $json['city'];
?>

Edited

Change freegeoip.net into ipinfo.io

<?php    

function get_client_ip()
{
    $ipaddress = '';
    if (isset($_SERVER['HTTP_CLIENT_IP'])) {
        $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
    } else if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else if (isset($_SERVER['HTTP_X_FORWARDED'])) {
        $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
    } else if (isset($_SERVER['HTTP_FORWARDED_FOR'])) {
        $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
    } else if (isset($_SERVER['HTTP_FORWARDED'])) {
        $ipaddress = $_SERVER['HTTP_FORWARDED'];
    } else if (isset($_SERVER['REMOTE_ADDR'])) {
        $ipaddress = $_SERVER['REMOTE_ADDR'];
    } else {
        $ipaddress = 'UNKNOWN';
    }

    return $ipaddress;
}
$PublicIP = get_client_ip();
$json     = file_get_contents("http://ipinfo.io/$PublicIP/geo");
$json     = json_decode($json, true);
$country  = $json['country'];
$region   = $json['region'];
$city     = $json['city'];
?>
如梦亦如幻 2024-11-02 05:38:31
<?php
    $query = @unserialize (file_get_contents('http://ip-api.com/php/'));
    if ($query && $query['status'] == 'success') {
        echo 'Hey user from ' . $query['country'] . ', ' . $query['city'] . '!';
    }
    foreach ($query as $data) {
        echo $data . "<br>";
    }
?>

使用此源尝试此代码。 *有用!

<?php
    $query = @unserialize (file_get_contents('http://ip-api.com/php/'));
    if ($query && $query['status'] == 'success') {
        echo 'Hey user from ' . $query['country'] . ', ' . $query['city'] . '!';
    }
    foreach ($query as $data) {
        echo $data . "<br>";
    }
?>

Try this code using this source. *It works!

我的影子我的梦 2024-11-02 05:38:31

使用 hostip.info 服务尝试此代码:

$country=file_get_contents('http://api.hostip.info/get_html.php?ip=');
echo $country;

// Reformat the data returned (Keep only country and country abbr.)
$only_country=explode (" ", $country);

echo "Country : ".$only_country[1]." ".substr($only_country[2],0,4);

Try this code using the hostip.info service:

$country=file_get_contents('http://api.hostip.info/get_html.php?ip=');
echo $country;

// Reformat the data returned (Keep only country and country abbr.)
$only_country=explode (" ", $country);

echo "Country : ".$only_country[1]." ".substr($only_country[2],0,4);
七色彩虹 2024-11-02 05:38:31

由于PHP依赖服务器,无法提供实时位置。只能提供静态位置。最好避免依赖 JavaScript 来定位,而不是使用 PHP。

但是需要将 JavaScript 数据发布到 PHP,以便服务器上的程序可以轻松访问它。

As PHP relies on a server, the real-time location can’t be provided. Only a static location can be provided. It is better to avoid to rely on the JavaScript for a location rather than using PHP.

But there is a need to post the JavaScript data to PHP, so that it can easily be accessible to a program on the server.

烟─花易冷 2024-11-02 05:38:31

IP 地址为您提供了一个相当不可靠的位置。如果一开始获取位置并不重要,则可以在加载时使用 JavaScript Ajax 位置。 (此外,用户需要授予您访问权限。)

HTML5 地理位置

An IP address gives you a quite unreliable location. You could Ajax the location upon load with JavaScript if it isn't critical to have the location at first. (Also, the user needs to give you its permission to access it.)

HTML5 Geolocation

执笔绘流年 2024-11-02 05:38:31

旧的 freegeoip API 现已弃用,并将于 2018 年 7 月 1 日停止使用。

新的 API 来自ipstack。您必须在 ipstack 中创建帐户。然后您可以使用 API URL 中的访问密钥。

$url = "http://api.ipstack.com/122.167.180.20?access_key=ACCESS_KEY&format=1";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
$city  = $response->city; // You can get all the details like longitude, latitude from the '$response'.

有关更多信息,请查看freegeoip (GitHub)。

The old freegeoip API is now deprecated and will be discontinued on 2018-07-01.

The new API is from ipstack. You have to create the account in ipstack. Then you can use the access key in the API URL.

$url = "http://api.ipstack.com/122.167.180.20?access_key=ACCESS_KEY&format=1";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response = json_decode($response);
$city  = $response->city; // You can get all the details like longitude, latitude from the '$response'.

For more information, check at freegeoip (GitHub).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文