4

This question may look as a duplicate one, but the existing questions did not resolve my need.

I have checked so many questions on this topic in Stackoverflow and in Google, but I couldn't find the efficient method. Almost in all the threads people have suggested this site http://www.hostip.info/ to detect the user location. And when I try to check that site, it said me "No clue about your location". And in some other sites, my city is determined based on my IP address and it is wrong.

But in google.com or bing.com, the sites determined my location exactly. How they do that? How can I get the visitor location (just the city is enough) exactly? And in some sites I saw the Google Chrome throws a question to me, "Allow this site to share your location". How they use this feature?

4

3 回答 3

9

The answer is "BY IP ADDRESS OF MACHINE / GEO-LOCATION"

There is a plugin written in php for do such task very easily,

Step-1) Download plugin from 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";
?>

For more details check out plugin website : click here.

NOTE : If you wanna do same thing by your way you need a data of IP address locations. Then after you can find it.

于 2013-10-08T12:17:13.520 回答
2

Excellent services:

1) http://ipinfo.io/123.123.123.123

2) http://ip-api.com/php/123.123.123.123

3) detect country & city (PHP, GeoIP Maxmind)

于 2016-08-02T12:10:33.727 回答
1

Easy and simple code to detect city o country i hope to help you

    $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>");
于 2015-06-15T12:34:26.973 回答