Viswalinga Surya S Viswalinga Surya S - 1 year ago 66
PHP Question

How to detect the user city efficiently?

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 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 or, 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?

Answer Source


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

Step-1) Download plugin from here.

Step-2) Little code snippet

    $geoplugin = new geoPlugin();

    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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download