Get Location from IP address

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
youropensource
Forum Commoner
Posts: 26
Joined: Tue Aug 05, 2008 11:42 am

Get Location from IP address

Post by youropensource »

how to get the location( city) from IP address in PHP ?

how to develop the code for that ?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Get Location from IP address

Post 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.
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Get Location from IP address

Post 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>
youropensource
Forum Commoner
Posts: 26
Joined: Tue Aug 05, 2008 11:42 am

Re: Get Location from IP address

Post 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 ?
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: Get Location from IP address

Post 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.
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Get Location from IP address

Post 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.
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Get Location from IP address

Post 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.
youropensource
Forum Commoner
Posts: 26
Joined: Tue Aug 05, 2008 11:42 am

Re: Get Location from IP address

Post by youropensource »

Anyone know Where I can get the database of IPs with (city,country.etc.,) details ?
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: Get Location from IP address

Post by omniuni »

The maxmind site explains, see my post above.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Get Location from IP address

Post 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.
Post Reply