php 获取用户当前位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5398674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:16:22  来源:igfitidea点击:

Get user's current location

phpgeolocationip

提问by Jacek Francuz

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

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

回答by Hamza

<?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"];
?>

回答by NaveenDA

Edited

已编辑

Change freegeoip.netinto ipinfo.io

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'];
?>

回答by Adnan Rasheed

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

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

回答by Bedh

Try this code using the hostip.infoservice:

使用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);

回答by glomad

MaxMind GeoIPis a good service. They also have a free city-level lookup service.

MaxMind GeoIP是一项很好的服务。他们还有免费的城市级查询服务。

回答by madkris24

You may want to take a look at GeoIP Country Whois Locatorfound at PHPClasses.

您可能想看看在PHPClasses 中找到的GeoIP Country Whois Locator

回答by Venkat Sai

as PHP relies on server, the real-time location cant be provided only static location can be provided it is better to avoid to rely on the JS for location rather than using php. But there is a need to post the js data to php so that it can be easily be accesible to program on server

由于PHP依赖服务器,无法提供实时位置,只能提供静态位置,最好避免依赖JS定位,而不是使用php。但是需要将js数据发布到php中,这样就可以很容易地在服务器上进行编程

回答by Sam Pettersson

An IP gives you an quite unreliable location, you could Ajax the location upon load with JS if it isn't critical to have the location at first. (Also, the user need's to give you it's permission to access it.)

IP 为您提供了一个非常不可靠的位置,如果一开始位置并不重要,您可以在加载时使用 JS Ajax 位置。(此外,用户需要授予您访问它的权限。)

Html5 Geolocation

HTML5 地理定位

回答by lalithkumar

The old freegeoip API is now deprecated and will be discontinued on July 1st, 2018.

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

The new API is from https://ipstack.com. You have to create the account in ipstack.Then you can use the access key in the API url.

新 API 来自https://ipstack.com。您必须在 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 .

For more information check here :/ https://github.com/apilayer/freegeoip

有关更多信息,请查看此处:/ https://github.com/apilayer/freegeoip