如何有效地检测用户城市?

时间:2013-10-08 11:57:57

标签: php location

这个问题可能看起来很复杂,但现有的问题并没有解决我的需要。

我在Stackoverflow和Google中检查了很多关于此主题的问题,但我找不到有效的方法。几乎在所有线程中,人们都建议此站点http://www.hostip.info/来检测用户位置。当我试图查看该网站时,它说我“没有关于你的位置的线索”。在其他一些网站中,我的城市是根据我的IP地址确定的,这是错误的。

但是在google.com或bing.com上,网站确切地确定了我的位置。他们是怎么做到的?我怎样才能确切地获得游客的位置(这个城市就足够了)?在某些网站上,我看到Google Chrome向我提出一个问题,“允许此网站分享您的位置”。他们如何使用此功能?

3 个答案:

答案 0 :(得分:6)

  

答案是"通过机器/地理位置的IP地址"

有一个用php编写的插件可以很容易地完成这个任务,

步骤1)从here.

下载插件

Step-2)Little code snippet

<?php
    require_once('geoplugin.class.php');
    $geoplugin = new geoPlugin();
    $geoplugin->locate();

    echo "Geolocation results for {$geoplugin->ip}: <br />\n".
        "City: {$geoplugin->city} <br />\n".
        "Region: {$geoplugin->region} <br />\n".
        "Area Code: {$geoplugin->areaCode} <br />\n".
        "DMA Code: {$geoplugin->dmaCode} <br />\n".
        "Country Name: {$geoplugin->countryName} <br />\n".
        "Country Code: {$geoplugin->countryCode} <br />\n".
        "Longitude: {$geoplugin->longitude} <br />\n".
        "Latitude: {$geoplugin->latitude} <br />\n".
    "Currency Code: {$geoplugin->currencyCode} <br />\n".
    "Currency Symbol: {$geoplugin->currencySymbol} <br />\n".
    "Exchange Rate: {$geoplugin->currencyConverter} <br />\n";
?>

有关详细信息,请查看插件网站:click here.

  

注意:如果你想按自己的方式做同样的事情,你需要一个IP数据   地址位置。然后你就可以找到它。

答案 1 :(得分:2)

答案 2 :(得分:1)

检测城市或国家的简单易用的代码我希望能帮助您

    $geoplugin = unserialize( file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $_SERVER['REMOTE_ADDR']) );
   echo ("<h1>".$geoplugin['geoplugin_countryCode']."</h1> <br>");
   echo ("<h1>".$geoplugin['geoplugin_countryName']."</h1> <br>");
   echo ("<h1>".$geoplugin['geoplugin_regionCode']."</h1> <br>");
   echo ("<h1>".$geoplugin['geoplugin_regionName']."</h1><br>");
   echo ("<h1>".$geoplugin['geoplugin_currencyCode']."</h1><br>");
   echo ("<h1>".$geoplugin['geoplugin_currencySymbol']."</h1><br>");
   echo ("<h1>".$geoplugin['geoplugin_currencySymbol_UTF8']."</h1><br>");
   echo ("<h1>".$geoplugin['geoplugin_currencyConverter']."</h1>");
相关问题