Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting Country

Status
Not open for further replies.

DonP

IS-IT--Management
Joined
Jul 20, 2000
Messages
684
Location
US
Is there some way to fairly accurately get the country in which is visitor to a site resides? I'm not interested in the language code, just the two-character country code. Any help is appreciated. Don
don@pc-homepage.com
Experienced in HTML, Perl, VBScript, PWS, IIS and Apache. Run OS/2 Warp 4, BeOS v5 and Windows NT/2000 (only when I have to!)
 
If you are using Apache:
$_SERVER[REMOTE_ADDR] gives you the IP.
You can use that to do a gethostbyaddr():

$host = gethostbyaddr($_SERVER[REMOTE_ADDR]);
$at        = $host
$result    = strrchr($at, '.');
echo "The right-most identifier is $result";


However, when the lookup fails, the IP is returned unmodified.
 
Doesn't this work only when it's a .uk or whatever? If so, then it would not give results for .com or anything else that doesn't have the country extension and won't work for my needs, which requires fairly good accuracy. I'll try it tonight but it looks very much like something I already tried from the PHP website.

I am running Apache but for some reason, gethostbyaddr() gives an error. My development platform has Apache2 on Win2K Server while the live site is through an ISP with a slightly older version, and PHP v3.22.25 with no option to upgrade. Don
don@pc-homepage.com
Experienced in HTML, Perl, VBScript, PWS, IIS and Apache. Run OS/2 Warp 4, BeOS v5 and Windows NT/2000 (only when I have to!)
 
You are absolutely right.
There is no foolproof way to ascertain a visitors country of origin. The best you can do is make an educated guess using techniques as the one suggested.

 
There is no deterministic way to get a visitor's country. As you have said, top-level domains like .com and .org are international in their extent. Also, the gethostbyaddre() function depends on system administrators' setting up reverse DNS for all addresses on their systems, which may not necessarily happen.

Another route, but one much more intensive in terms of network time and data processing, is to perform whois lookups against databases maintained by ARIN and its counterparts. Those registries can tell you to which organization ranges of IP addresses have been assigned. This page gives you an executive summary of what ARIN provides: Want the best answers? Ask the best questions: TANSTAAFL!
 
Thanks! I thought this would be the case so I guess I'll have to leave the site as it is where a first time visitor has to select their country before entering. Don
don@pc-homepage.com
Experienced in HTML, Perl, VBScript, PWS, IIS and Apache. Run OS/2 Warp 4, BeOS v5 and Windows NT/2000 (only when I have to!)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top