Get Location from IP address
Moderator: General Moderators
-
youropensource
- Forum Commoner
- Posts: 26
- Joined: Tue Aug 05, 2008 11:42 am
Get Location from IP address
how to get the location( city) from IP address in PHP ?
how to develop the code for that ?
how to develop the code for that ?
Re: Get Location from IP address
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.youropensource wrote:how to get the location( city) from IP address in PHP ?
how to develop the code for that ?
Re: Get Location from IP address
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:
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>-
youropensource
- Forum Commoner
- Posts: 26
- Joined: Tue Aug 05, 2008 11:42 am
Re: Get Location from IP address
Thanks omniuni.
May I know whether the IPs data is free or cost ?
also let me know where I get/buy the IPs data ?
May I know whether the IPs data is free or cost ?
also let me know where I get/buy the IPs data ?
- jayshields
- DevNet Resident
- Posts: 1912
- Joined: Mon Aug 22, 2005 12:11 pm
- Location: Leeds/Manchester, England
Re: Get Location from IP address
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
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
By the way, the service is free from http://www.hostip.info/
This line:
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.
This line:
Code: Select all
$hostIP_results = file_get_contents("http://api.hostip.info/get_html.php?ip=".getenv("REMOTE_ADDR"));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.
-
youropensource
- Forum Commoner
- Posts: 26
- Joined: Tue Aug 05, 2008 11:42 am
Re: Get Location from IP address
Anyone know Where I can get the database of IPs with (city,country.etc.,) details ?
Re: Get Location from IP address
The maxmind site explains, see my post above.
Re: Get Location from IP address
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.