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
$city= ucfirst($tags['city']); // $tags is an array that contains city name for the supplied IP Address
echo $city; // prints the city name
Fantastic idea and brilliantly explained. Keep up with the good work!
ReplyDeleteThank you Buddy! I will :).
ReplyDeletehow can i get the api
ReplyDelete@Bharath: you don't need to get the API. Just follow the above script which uses API link from Geobytes.
ReplyDeleteNice information . I would like to add an alternate code snippet here to find location using ip address :
ReplyDelete$ip = $_SERVER['REMOTE_ADDR'];
$details = json_decode(file_get_contents("http://ip-details.com.io/{$ip}/json"));
echo $details->city;
Source : Ip-details.com