Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Finding out which country a user comes from using $_ENV[HTTP_...]

by meetn2veg (Scribe)
on Oct 09, 2002 at 20:33 UTC ( [id://204042]=perlquestion: print w/replies, xml ) Need Help??

meetn2veg has asked for the wisdom of the Perl Monks concerning the following question:

I know there's a way....

By using one of the UNIX $_ENV[HTTP_...] vars, you can find out (more or less) which country a visitor comes from.

Any ideas on which $_ENV var does this? I know it has something to do with resolving the IP address and also if the server as it activated, but....

Thanks again

Replies are listed 'Best First'.
Re: Finding out which country a user comes from using $_ENV[HTTP_...]
by dws (Chancellor) on Oct 09, 2002 at 21:01 UTC
    By using one of the UNIX $_ENV[HTTP_...] vars, you can find out (more or less) which country a visitor comes from.

    REMOTE_ADDR give you the client's IP address, but you still have a long way to go to map this to a country. You'll need to get your hands on IANA (Internet Assigned Number Authority) data, and data from the other organizations AINA has delegated to, parse it, and build an access method.

    People like Quova have build a business around this problem.

Re: Finding out which country a user comes from using $_ENV[HTTP_...]
by Rex(Wrecks) (Curate) on Oct 09, 2002 at 21:03 UTC
    You might want to have a look at Visual Route for ideas. They seem to have a way to pinpoint location via traceroute, and using the same method might give you ideas.

    "Nothing is sure but death and taxes" I say combine the two and its death to all taxes!
Re: Finding out which country a user comes from using $_ENV[HTTP_...]
by grantm (Parson) on Oct 09, 2002 at 23:45 UTC

    Apache has a setting called: 'HostnameLookups' which defaults to 'Off'. If you change this to 'On', then you can get the fully qualified domain name of the client rather than it's IP address from the environment. Don't do this. This will slow down all requests to your server and increase the load. It will not be able to resolve addresses for which there is no reverse lookup. There is no guarantee that the domain name will bear any resemblance to the country the user is in anyway (ISPs the world over use .net addresses for example).

    You could also look at the Accept-language header to get some clue but that's not likely to be much more successful (but at least the overhead will be lower).

Re: Finding out which country a user comes from using $_ENV[HTTP_...]
by rob_au (Abbot) on Oct 10, 2002 at 10:58 UTC
    As others have pointed out, I would reiterate the point that there is no guaranteed method by which to determine the origin of country of visitors to a web site. Nevertheless, I would direct your attention to the Geo::IP module on CPAN which uses a file-based database with IP address blocks as keys and country of assignment as values.

    An example piece of code using this module:

    use CGI; use Geo::IP; my $cgi = CGI->new; my $geo = Geo::IP->new; my $ip_addr = gethostbyname( $cgi->remote_host ); my $country = $geo->country_code_by_addr( inet_ntoa( $ip_addr ) );

    Please note that this method is not absolute, however should provide a relatively robust and ready-made solution.

     

    perl -e 'print+unpack("N",pack("B32","00000000000000000000000111001100")),"\n"'

Re: Finding out which country a user comes from using $_ENV[HTTP_...]
by void (Scribe) on Dec 11, 2002 at 18:41 UTC
    I know this is kind of an old post, but have a look here.

    A perl CGI that will route based on the IP of the visitor.

    "The Universe is not only more complex than we imagine, it is more complex than we can imagine." - Albert Einstein
Re: Finding out which country a user comes from using $_ENV[HTTP_...]
by George_Sherston (Vicar) on May 01, 2003 at 16:33 UTC
    I found this thread when trying to answer the same question. As rob_au suggested, I tried Geo::IP but my ISP doesn't support it (they have the module, but it needs a separate database file that has to be updated). I didn't have the necessary expertise to install the current version (it comes with a C library) and I couldn't make Geo::IP::PurePerl work. In the end I plumped for Geo::IPFree which I installed without trouble. It has a slightly simpler interface if all you want is to extract the country name and short code:
    use CGI; use Geo::IPfree qw(LookUp); my ($countrycode,$country) = LookUp($cgi->remote_host); print $countrycode,"\n",$country;
    Returns, for example:
    GB United Kingdom


    § George Sherston

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://204042]
Approved by VSarkiss
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-03-28 19:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found