Page 1 of 1
Get Location from IP address
Posted: Fri Oct 31, 2008 9:01 am
by youropensource
how to get the location( city) from IP address in PHP ?
how to develop the code for that ?
Re: Get Location from IP address
Posted: Fri Oct 31, 2008 3:10 pm
by califdon
youropensource wrote:how to get the location( city) from IP address in PHP ?
how to develop the code for that ?
It's not a matter of code. Obviously, the information must be obtained from some database. Google for
geo ip and you will find many sources.
Re: Get Location from IP address
Posted: Fri Oct 31, 2008 9:48 pm
by omniuni
I will mention that I did work out a script for this.... I will try to locate it, but it was unreliable due to companies moving around IP addresses.
Let me know if
http://d-site.net/stuff/test/location.php you can test out.
Here's the code:
Code: Select all
<?php
$hostIP_results = file_get_contents("http://api.hostip.info/get_html.php?ip=".getenv("REMOTE_ADDR"));
$LOCATION['country'] = substr($hostIP_results, strpos($hostIP_results, "(")+1, strpos($hostIP_results, ")")-(strpos($hostIP_results, "(")+1));
$LOCATION['city'] = substr($hostIP_results, strpos($hostIP_results, "City:")+6);
$LOCATION_CT_ST = $LOCATION['city'].", ".$LOCATION['country'];
?>
<html>
<head>
<title>Show My Location</title>
</head>
<body>
<h2>Your Location: <?php echo $LOCATION_CT_ST; ?></h2>
<p>
</body>
</html>
Re: Get Location from IP address
Posted: Mon Nov 03, 2008 3:30 am
by youropensource
Thanks omniuni.
May I know whether the IPs data is free or cost ?
also let me know where I get/buy the IPs data ?
Re: Get Location from IP address
Posted: Mon Nov 03, 2008 5:50 am
by jayshields
I think that script shows the location of your ISP rather than the end-user. Atleast for me it says London, whereas I've seen targeted ads for my actual city quite a few times before.
Re: Get Location from IP address
Posted: Mon Nov 03, 2008 7:36 am
by omniuni
It sometimes shows a city, sometimes a country, sometimes someplace else all-together. ISP's sometimes give people IP addresses from other areas and never update the database. There is no law that they have to, so locating a person based on IP is finicky at best. That said, some of your problem might be that I am targeting city and STATE out of the information sent back from the site. I'm in the U.S. so I wrote the script for local testing. If you want, you can look at the other information that the site gives, and you might find what you're looking for there.
Re: Get Location from IP address
Posted: Mon Nov 03, 2008 7:43 am
by omniuni
By the way, the service is free from
http://www.hostip.info/
This line:
Code: Select all
$hostIP_results = file_get_contents("http://api.hostip.info/get_html.php?ip=".getenv("REMOTE_ADDR"));
fetches the output of their script which gives you the location data. You can use the API directly to get XML instead of text:
http://api.hostip.info/?ip=66.162.207.114
Again, just replace the IP with the one to match, as shown in the code, and you need only parse the results for that user.
Also, I'm currently at my Dad's office in Raleigh, North Carolina, and the output of my script is:
Your Location: Raleigh, NC, US
-OmniUni
P.S. HostIP.info recommends maxmind.com for a commercial solution: they seem to be extremely accurate, if you want to pay a few hundred dollars a month. That said, visit
http://www.maxmind.com/app/geolitecity for their "free" option... you'll just need to download the 100MB CSV every month.
Re: Get Location from IP address
Posted: Mon Nov 03, 2008 8:06 am
by youropensource
Anyone know Where I can get the database of IPs with (city,country.etc.,) details ?
Re: Get Location from IP address
Posted: Tue Nov 04, 2008 2:14 pm
by omniuni
The maxmind site explains, see my post above.
Re: Get Location from IP address
Posted: Tue Nov 04, 2008 2:43 pm
by califdon
And if you've been reading the answers that we've offered to you, you will note that I referred you to Google, where you will find lots of sources.