By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,480 Members | 1,164 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,480 IT Pros & Developers. It's quick & easy.

Finding geographic location of IP address

P: n/a
Does anyone have a favored way of finding the geographic location
of a user's IP address, so that a php script can include content
relevant to that location?

Reverse-lookup of the hostname isn't reliable and often doesn't
work, especially if the returned hostname ends with .com, .net,
..info, .biz or other tld that doesn't reveal geographic location.

I also see a bunch of commercial services that offer a "country
whois" function for a subscription fee. You'd think this sort of
capability would be standardized by now.

Is anybody doing this? If so, how?

-A
Aug 16 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Rik
On Thu, 16 Aug 2007 20:48:58 +0200, axlq <ax**@spamcop.netwrote:
Does anyone have a favored way of finding the geographic location
of a user's IP address, so that a php script can include content
relevant to that location?

Reverse-lookup of the hostname isn't reliable and often doesn't
work, especially if the returned hostname ends with .com, .net,
.info, .biz or other tld that doesn't reveal geographic location.

I also see a bunch of commercial services that offer a "country
whois" function for a subscription fee. You'd think this sort of
capability would be standardized by now.
Hardly. Giving out IP's has little to do with location. It's pretty hard
work keeping it up to date. There's hardly any chance of something really
reliable, and certainly not for free.
Is anybody doing this? If so, how?
By using a database updated by a third party, with information more or
less accurate most of the time:
<http://www.maxmind.com/app/geolitecity>
--
Rik Wasmus
Aug 16 '07 #2

P: n/a
NC
On Aug 16, 11:48 am, a...@spamcop.net (axlq) wrote:
>
Does anyone have a favored way of finding the geographic location
of a user's IP address, so that a php script can include content
relevant to that location?
Nope. IP addresses often lie. AOL users in the U.S. all appear to
come from Virginia. AOL users in Germany are often seen as coming
from the UK. Users on corporate networks are usually seen as located
at the corporation's headquarters or even the corporation's telco's
headquarters.
I also see a bunch of commercial services that offer a "country
whois" function for a subscription fee. You'd think this sort of
capability would be standardized by now.
It is. Matching IP addresses with locations is done based on WHOIS
information. But to get and keep current WHOIS for the entire
Internet, you need to be a domain registrar or good friends with
one. :) Plus, WHOIS reflects who owns the IP address, not who uses
it...

Cheers,
NC

Aug 16 '07 #3

P: n/a
Rik
On Fri, 17 Aug 2007 00:02:59 +0200, NC <nc@iname.comwrote:
>I also see a bunch of commercial services that offer a "country
whois" function for a subscription fee. You'd think this sort of
capability would be standardized by now.

It is. Matching IP addresses with locations is done based on WHOIS
information.
They might use them, however, mathing IP-adresses to country (state, city)
is not entirely done by WHOIS information. It is often a fallback if
nothing else is known/certain. A lot of the more reliable sources
continuously hold massive 'surveys' (do you live here? if not, please tell
us where you do...), and get information from third parties where an
IP-address and geolocation are given (some ISP's and larger online stores
for example, allthough they would have to have a very precise privacy
statement IMHO). Millions of datasets get processed to extrapolate some
probable range of ip's for a location, in which case the whois information
comes in handy for determening which ISP's hold certain ranges. That's
more of a 'boundary'-determining of a range then the actual 'value' of a
range. AOL is certainly hard to pin down due to their proxy network.

It's all quite a lot of work, and terribly expensive.

And on a sidenote in this thread a warning to all trying to use
geolocation to determine language: don't, use the language-accept headers.
Geolocation might be usefull to provide 'local' information or to
determine the general location of (groups of) clients.
--
Rik Wasmus
Aug 16 '07 #4

P: n/a
In article <op.tw51e6taqnv3q9@metallium>,
Rik <lu************@hotmail.comwrote:
>Is anybody doing this? If so, how?

By using a database updated by a third party, with information more or
less accurate most of the time:
<http://www.maxmind.com/app/geolitecity>
Thanks. Gag... scan through a 28 megabyte database just to find my
user's city? All I need are countries, and I see that site has a
country databse. Thanks.

-A
Aug 17 '07 #5

P: n/a
In article <op.tw6bb5g7qnv3q9@metallium>,
Rik <lu************@hotmail.comwrote:
[snip explanation of effort involved maintaining an IP country database]

Thanks. I didn't realize it was so involved.
>And on a sidenote in this thread a warning to all trying to use
geolocation to determine language: don't, use the language-accept
headers. Geolocation might be usefull to provide 'local'
information or to determine the general location of (groups of)
clients.
All I need it for is to make an educated guess about the user's
country, just so a selection box will default to the right type of
currency for exchange-rate calculations. Geolocation isn't even
necessary for the site I'm building but I thought it would be a
user-friendly touch to have certain defaults based on country.

-A
Aug 17 '07 #6

P: n/a
On Aug 16, 11:48 pm, a...@spamcop.net (axlq) wrote:
Does anyone have a favored way of finding the geographic location
of a user's IP address, so that a php script can include content
relevant to that location?
<snip>
1. http://ip-to-country.webhosting.info/
2. http://groups.google.com/group/comp....2732449830364d

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Aug 18 '07 #7

P: n/a
here's a straight-forward method to begin with. any other issues raised can
be answered easily with a little investigation and modification to this
general approach. the best you can do is find the issuing isp. most of the
time, the user of the ip is very near the issuer.

hih,

me

==================

<?
class isp
{
public $address = '';
public $city = '';
public $clientIp = '';
public $country = '';
public $email = '';
public $name = '';
public $phone = '';
public $state = '';
public $zip = '';

private function __clone(){}

public function __construct($ip = '')
{
if (!$ip)
{
if (!$ip = $_SERVER['HTTP_CLIENT_IP'])
{
if (!$ip = $_SERVER['HTTP_X_FORWARDED_FOR'])
{
if (!$ip = $_SERVER['REMOTE_ADDR']){ $ip = ''; }
}
}
}
$this->clientIp = $ip;
$query =
file_get_contents("http://ws.arin.net/cgi-bin/whois.pl?queryinput=$ip");
if (strstr($query, "No match")){ return; }
$this->name = $this->getSegment('OrgName:', $query);
if ($isp->isp == '')
{
$href = preg_match('/HREF="([^"]*)"/', $query, $uri);
$href = 'http://ws.arin.net' . $uri[1];
$query = file_get_contents($href);
}

$this->address = strtoupper($this->getSegment('Address:' ,
$query));
$this->city = strtoupper($this->getSegment('City:' ,
$query));
$this->country = strtoupper($this->getSegment('Country:' ,
$query));
$this->email = strtolower($this->getSegment('OrgAbuseEmail:' ,
$query));
$this->name = strtoupper($this->getSegment('OrgName:' ,
$query));
$this->phone = strtoupper($this->getSegment('OrgAbusePhone:' ,
$query));
$this->state = strtoupper($this->getSegment('StateProv:' ,
$query));
$this->zip = strtoupper($this->getSegment('PostalCode:' ,
$query));
}

private function getSegment($segment, $source)
{
$pattern = '/' . $segment . '([^\n]*)\n/i';
$segment = preg_match($pattern, $source, $matches);
$segment = preg_replace('/<[^>]*>/', '', $matches[1]);
return $segment;
}
}
?>
Aug 18 '07 #8

P: n/a
On Aug 19, 3:20 am, "Steve" <no....@example.comwrote:
here's a straight-forward method to begin with. any other issues raised can
be answered easily with a little investigation and modification to this
general approach. the best you can do is find the issuing isp. most of the
time, the user of the ip is very near the issuer.
<snip>
$href = 'http://ws.arin.net'. $uri[1];
<snip>

ws.arin.net host won't return whois info for all IP blocks. See my
post above.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Aug 19 '07 #9

P: n/a
In article <Yg*************@newsfe12.lga>, Steve <no****@example.comwrote:
>here's a straight-forward method to begin with. any other issues raised can
be answered easily with a little investigation and modification to this
general approach. the best you can do is find the issuing isp. most of the
time, the user of the ip is very near the issuer.
[snip example to get country information from whois server like ARIN]

That's an interesting approach, but seems impractical to me. Most
whois servers will cut you off if you have too many requests in one
day. For a high-traffic site that needs geolocation information, a
local database may be best.

-A
Aug 19 '07 #10

P: n/a
Try http://freeipservices.com
On Aug 16, 11:48 am, a...@spamcop.net (axlq) wrote:
Does anyone have a favored way of finding the geographic location
of a user'sIPaddress, so that a php script can include content
relevant to that location?

Reverse-lookup of the hostname isn't reliable and often doesn't
work, especially if the returned hostname ends with .com, .net,
.info, .biz or other tld that doesn't reveal geographic location.

I also see a bunch of commercial services that offer a "country
whois" function for a subscription fee. You'd think this sort of
capability would be standardized by now.

Is anybody doing this? If so, how?

-A

Aug 23 '07 #11

P: n/a

"tanuj96" <ta*************@gmail.comwrote in message
news:11**********************@j4g2000prf.googlegro ups.com...
| Try http://freeipservices.com

except it's a completely bad sign when they use my ip and show i'm from san
fransisco when i'm more than half way across a continent.

ip-address.com got it correct. there are tons of others. again, you could
use them instead of a whois. the parsing code i wrote would do fine to get
data from the mirad out there. worried about copious calls to it? get a list
of those you want to use and select from them randomly. if one doesn't turn
up geo info (changed the tags the info is in, didn't have any info, etc.)
move to the next. it would be far easier to notice that a site's tags have
changed than trying to keep up a db with current information...unless you
want to pay, which i'm not to into doing.

of course others have different thoughts on all of that. i'll go with
whatever my employer wants, but if it's just for me...parsing/scraping is
the lightweight, free approach that only requires you to monitor the tags on
the page(s) you're scraping...and you can have your system notify you when
it appears successive requests are turning up blanks...hell, before you get
your panties in a wad, you can even turn around automatically and test those
with a known ip (yours) and if it's blank, email or otherwise alert yourself
to get things back in line with that page's tags.

but i digress...
Aug 23 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.