Location detecting techniques for IP addresses
What are the location detecting techniques for IP addresses?
I know to look at the$_SERVER['HTTP_ACCEPT_LANGUAGE']
(not accurate but mostly useful to detect location, for example if an IP range's users set French to their browser then it means that this range) belongs to France
andgethostbyaddr($_SERVER['REMOTE_ADDR'])
(to look country code top-level domain)
then may be to whois gethostbyaddr($_SERVER['REMOTE_ADDR'])
sometimes:$HTTP_USER_AGENT
(Firefox's user agent string has language code, not accurate but mostly can be used to detect the location)
Also I know how to get the time zone but it does not work in the new browsers. Moreover there is css issue that detects visitor's history, it can be used to see what google and wikipedia pages he/she has visited (google.co.uk, google.com.tr)
But what about cities?
You can't do this without a database that maps IP addresses to cities/countries/providers. There are commercial offerings such as ip2location that you could use. AFAIK there is no free alternative though, as maintaining such a IP database is quite a lot of work. Free alternative: GeoIP2
Update: There are several things that allow you to create such a db, if you invest enough time:
- Use the databases provided by regional and local registries to find an IP's owner.
- Many ISPs use a naming schema that allows you to locate the user. Sometimes you can even read the city name in plain text if you do a reverse-DNS lookup. Sometimes it is more cryptic. For example I currently have p5dcf6c4a.dip.t-dialin.net , and I have no idea that the naming scheme is..
- Use a traceroute. If you can't identify the location of a user, you can still find out the location of its uplink
If you looking for some copy - paste solution, i foudn this code :
function countryCityFromIP($ipAddr)
{
//function to find country and city from IP address
//Developed by Roshan Bhattarai http://roshanbh.com.np
//verify the IP address for the
ip2long($ipAddr)== -1 || ip2long($ipAddr) === false ? trigger_error("Invalid IP", E_USER_ERROR) : "";
$ipDetail=array(); //initialize a blank array
//get the XML result from hostip.info
$xml = file_get_contents("http://api.hostip.info/?ip=".$ipAddr);
//get the city name inside the node <gml:name> and </gml:name>
preg_match("@<Hostip>(\s)*<gml:name>(.*?)</gml:name>@si",$xml,$match);
//assing the city name to the array
$ipDetail['city']=$match[2];
//get the country name inside the node <countryName> and </countryName>
preg_match("@<countryName>(.*?)</countryName>@si",$xml,$matches);
//assign the country name to the $ipDetail array
$ipDetail['country']=$matches[1];
//get the country name inside the node <countryName> and </countryName>
preg_match("@<countryAbbrev>(.*?)</countryAbbrev>@si",$xml,$cc_match);
$ipDetail['country_code']=$cc_match[1]; //assing the country code to array
//return the array containing city, country and country code
return $ipDetail;
}
Hope this helps somebody.
There are databases (some of them are free of charge) which can be used to convert IP's into Cities:
eg: http://www.maxmind.com/app/geolitecity
You can map an IP address to its location (city, country, postal code), timezone etc. with either a database or an API (hosted database).
Here's an example of using an API
curl https://ipapi.co/city/
> Mountain View
curl https://ipapi.co/country/
> US
curl https://ipapi.co/timezone/
> America/Los_Angeles
more here : Location from IP address