Getting city name using real IP address


Getting the name of the city using IP address is quite simple. This tutorial shows how we can get real IP address of the server and find the city name using the IP address with the help of API from GEOBYTES.
Following are the steps to do this:



1. Find the real IP address using the following function.

function getRealIpAddr()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
    {
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
    {
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
      $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

$ip=getRealIpAddr();

2. Get the city name using the API from GEOBYTES.

$tags = get_meta_tags("http://www.geobytes.com/IpLocator.htm?GetLocation&template=php3.txt&IpAddress=$ip");
$city= ucfirst($tags['city']); // $tags is an array that contains city name for the supplied IP Address
echo $city; // prints the city name

Comments

  1. Fantastic idea and brilliantly explained. Keep up with the good work!

    ReplyDelete
  2. Thank you Buddy! I will :).

    ReplyDelete
  3. @Bharath: you don't need to get the API. Just follow the above script which uses API link from Geobytes.

    ReplyDelete
  4. Nice information . I would like to add an alternate code snippet here to find location using ip address :

    $ip = $_SERVER['REMOTE_ADDR'];
    $details = json_decode(file_get_contents("http://ip-details.com.io/{$ip}/json"));
    echo $details->city;


    Source : Ip-details.com

    ReplyDelete

Post a Comment

Popular posts from this blog

Getting latitude, longitude, timezone using ip address.