-4

Can I detect the location of an IP address by PHP on my website?

If yes, which method should I use? I did not try any third party service. I want to detect location of IP address for navigation.

sheldonzy
  • 5,505
  • 9
  • 48
  • 86
  • Take a look at "geoip" libraries. They indeed rely on third parts data, no way around that. – arkascha Jun 15 '17 at 17:33
  • How accurate do you need it to be? Normally you can't get a better location than city. – Andreas Jun 15 '17 at 17:33
  • Any other way for accurate measurement?? – Saparya Rd Jun 15 '17 at 17:34
  • Sure, hack in to all ISPs in the world and plant a trojan there so you have access to all the data. – Andreas Jun 15 '17 at 17:35
  • 1
    Or place a adress field on the website and ask the user to fill it. – Andreas Jun 15 '17 at 17:36
  • 1
    https://stackoverflow.com/questions/11667838/how-do-i-get-an-addresses-latitude-longitude-using-html5-geolocation-or-google-a – Sammitch Jun 15 '17 at 17:39
  • There are many ways to do it, try this, https://stackoverflow.com/a/17864552/8167189 – Luis Bolivar Jun 15 '17 at 18:07
  • @Luis that link in the answer you link to is off by 11 km when I look up my IP. – Andreas Jun 15 '17 at 19:33
  • 4
    Possible duplicate of [Getting the location from an IP address](https://stackoverflow.com/questions/409999/getting-the-location-from-an-ip-address) – Syfer Jun 16 '17 at 01:15
  • Perhaps what you need is the [GeoLocation API](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation_API) that you can access from Javascript on your web page and pass the information back to your server. That should give you a location to witihn about 3m, regardless of IP address, if the user allows it for your site. – Tangentially Perpendicular Jul 10 '22 at 22:23

1 Answers1

0
function detect_city($ip) {
         
        $default = 'UNKNOWN';
 
        if (!is_string($ip) || strlen($ip) < 1 || $ip == '127.0.0.1' || $ip == 'localhost')
            $ip = '8.8.8.8';
 
        $curlopt_useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)';
         
        $url = 'http://ipinfodb.com/ip_locator.php?ip=' . urlencode($ip);
        $ch = curl_init();
         
        $curl_opt = array(
            CURLOPT_FOLLOWLOCATION  => 1,
            CURLOPT_HEADER      => 0,
            CURLOPT_RETURNTRANSFER  => 1,
            CURLOPT_USERAGENT   => $curlopt_useragent,
            CURLOPT_URL       => $url,
            CURLOPT_TIMEOUT         => 1,
            CURLOPT_REFERER         => 'http://' . $_SERVER['HTTP_HOST'],
        );
         
        curl_setopt_array($ch, $curl_opt);
         
        $content = curl_exec($ch);
         
        if (!is_null($curl_info)) {
            $curl_info = curl_getinfo($ch);
        }
         
        curl_close($ch);
         
        if ( preg_match('{<li>City : ([^<]*)</li>}i', $content, $regs) )  {
            $city = $regs[1];
        }
        if ( preg_match('{<li>State/Province : ([^<]*)</li>}i', $content, $regs) )  {
            $state = $regs[1];
        }
 
        if( $city!='' && $state!='' ){
          $location = $city . ', ' . $state;
          return $location;
        }else{
          return $default; 
        }
         
}
ahuemmer
  • 1,653
  • 9
  • 22
  • 29