Get country, city, region, location details based on IP Address | 2my4edge

08 October 2016

Get country, city, region, location details based on IP Address

Get country, city, region, lat, lang, country code, and location details based IP address. here we have PHP code to find the above all location details based on IP address. Based on IP address we can get more details about city, country, country code, latitude, longitude, region code, region name and some more details only based on IP address. Let see the code.

Get country city region location details based on IP Address
LIVE DEMO WITH ALL DETAILS                   WITH FEW DETAILS





PHP CODE DISPLAY ALL DETAILS BASED ON IP ADDRESS [DEMO]
<?php
function getiplocationdetails(){
    $remoteip  = @$_SERVER['REMOTE_ADDR'];
    $ip = $remoteip;
    $result = '';
    $ip_datails = @json_decode
              (file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));   
    return $ip_datails;
}


echo "<pre>";
print_r(getiplocationdetails()); 
?>

PHP CODE DISPLAY FEW DETAILS BASED ON IP ADDRESS [DEMO]
<?php
function getiplocationdetails(){
  $clientip  = @$_SERVER['HTTP_CLIENT_IP'];
  $forward   = @$_SERVER['HTTP_X_FORWARDED_FOR'];
  $remoteip  = @$_SERVER['REMOTE_ADDR'];
  $result  = array('country'=>'', 'city'=>'','region'=>'');
  if(filter_var($clientip, FILTER_VALIDATE_IP)){
      $ip = $clientip;
  }elseif(filter_var($forward, FILTER_VALIDATE_IP)){
      $ip = $forward;
  }else{
      $ip = $remoteip;
  }
  $ip_datails = @json_decode
     (file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));    
  if($ip_datails && $ip_datails->geoplugin_countryName != null){
      $result['country'] = $ip_datails->geoplugin_countryCode;
      $result['city'] = $ip_datails->geoplugin_city;
      $result['region'] = $ip_datails->geoplugin_region;
  }
  return $result;
}



echo '<pre>';
print_r(getiplocationdetails());
?>



I hope this code really helpful to you. keep visiting for more tutorials like this.

9 comments:

  1. Vinay Suthar10/08/2016 9:56 AM

    Wrong, I have tested demo and I am from ahmedabad and it's showing Mumbai....

    ReplyDelete
    Replies
    1. @Vinay Suthar. It will fetch the data only based on you Ip Address.

      Delete
    2. Vinay Suthar10/09/2016 9:07 PM

      Ohh Got It...
      Thanks Arun, for sharing this code...

      Delete
  2. Thanks for sharing this code, it works well

    ReplyDelete
  3. The above code getting delay in mobile. Any alternative solution?

    ReplyDelete
  4. Its working showing blank data.

    ReplyDelete
  5. Superior post, keep up with this exceptional work. It's nice to know that this topic is being also covered on this web site so cheers for taking the time to discuss this! Thanks again and again! IP Address

    ReplyDelete
  6. how can i get street address. ?

    ReplyDelete

^