Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect the user city efficiently?

Tags:

php

location

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?

like image 834
Sarvap Praharanayuthan Avatar asked Dec 15 '22 05:12

Sarvap Praharanayuthan


2 Answers

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.

like image 148
Hardik Thaker Avatar answered Jan 02 '23 21:01

Hardik Thaker


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>");
like image 25
Said Erraoudy Avatar answered Jan 02 '23 20:01

Said Erraoudy