Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

How to check the origin ofi a visitor?

by cranberry13 (Beadle)
on Jun 19, 2004 at 21:08 UTC ( [id://368206] : perlquestion . print w/replies, xml ) Need Help??

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

I need to determine which country a person is coming from. Isn't there an environment variable that can determine this for me? Do I need to take the ip address and do something with it? Any suggestions would be appreciated. Thank you. Some more notes: Basically, I *do not* want people in the middle east to see an 'advert' on one of our websites but I do want people in europe and America to see the advert. All the webpages on our website are 'dynamic' -- So I want to check approximately where the user is coming from. If they're in the middle east (Jordan, Israel, Saudi ARabia, etc) then do not display the advertisement. Anywhere else is fine. I do not want to ask the user where they are.

Replies are listed 'Best First'.
Re: How to check the origin ofi a visitor?
by Arunbear (Prior) on Jun 19, 2004 at 21:15 UTC
      Considering the original posters updated content, I would say that this is clearly the best answer. IP::Country won't be perfect and it won't match everyone in your target countries - but it will match the ones that can be resolved back to appropriate country codes. As I had mentioned elsewhere in this thread - if the person uses an ISP that is outside of their own country or SSHes into a machine outside of their country and fires up links or lynx, there's nothing you can do about it.
Re: How to check the origin ofi a visitor?
by arden (Curate) on Jun 19, 2004 at 21:53 UTC
    At what point do you want to know this information? Is it going to be when they are requesting a page that is being fed to them by a Perl script? Is it when you're going through the logs later on with a Perl script? How are they "coming" to your system? HTTP? FTP? Telnet?

    Now, to make a few assumptions: You are refering to HTTP traffic as it is being requested through CGI. In this case, you could either attempt to track down the IP number the requestor is giving using IP::Country, Apache::Geo::IP, or just Geo::IP, accepting the fact that some IP numbers are multi-national and also that a user can be in one location, requesting a document through a proxy located somewhere totally else.

    Another option is that you could check the values of Accept-Language and assume they are where their first choice is (although this is going to be set to US most of the time by default) or look within the User-Agent field for some value that might be set. Using this method (or one like it) you have to accept that the requestor can put bad data into the header, so it, too, cannot be guaranteed to be correct.

    - - arden.

Re: How to check the origin ofi a visitor?
by jaldhar (Vicar) on Jun 19, 2004 at 21:31 UTC

    You're possibly thinking of $LANG and the $LC* environment variables but they are about language and cultural features (such as currency symbols) not country.

    There are some methods for mapping IPs to countries but do not rely on them. For instance I noticed that many Indian ISPs resell accounts from US ISPs. A visitor from one of them would show up as American not Indian. So why not just ask instead of trying to guess?


Re: How to check the origin ofi a visitor?
by Seumas (Curate) on Jun 20, 2004 at 06:41 UTC
    No, there's no $ENV{} type of variable you can use for this. You can, however, do a reverse DNS lookup on their connecting IP and see what their TLD is, matching this against a list of ISO country abbreviations. This won't help you with .com, .net, .edu or .org of course.

    You can never be completely accurate since all you can verify is the region the IP address is registered in and the person themselves could be anywhere in the world.

    Your best bet would be to follow some of the other suggestions in this thread and using the mentioned CPAN packages. You could also take a look at decent web log analysis packages and see how they do it.

    It might be interesting to know exactly why you need to know this. If this is for user accounts, it will be useless since there is absolutely no reason your mailing address or residence would be in the same country as your IP address.
Re: How to check the origin ofi a visitor?
by Anonymous Monk on Jun 20, 2004 at 05:19 UTC
    ask the user.

      One does not ask the user if s/he wants to see an ad.

        One does not ask the user if s/he wants to see an ad.
        But if the user is told:
        Some content of this site has been tailored to specific countries or regions of the world. Help us serve you better by telling us where you are.
        It seems that there are "sensitive" ads that the OP only wants shown to certain regions. Or perhaps ads are regionalized, and showing Air Zooka ads in Jordan is pointless?

        It would help if there was non-ad content targeted regionally also - otherwise, no one would see the benefit (and might be blocking the ads anyway.)

        Quantum Mechanics: The dreams stuff is made of

        20040922 Edit by ysth: add http:// to link

Re: How to check the origin ofi a visitor?
by Roger (Parson) on Jun 19, 2004 at 23:11 UTC
    I would like to suggest a great software from Visualware called VisualRoute (which I am using). Great piece of software and you will not regret buying it. I have copied down a few notes of VisualRoute from the Visualware homepage:

  • Quickly locate the source of Internet delays! VisualRoute provides an automated analysis and quick identification of where Internet connectivity problems occur.
  • Stop getting the run around from your ISP! VisualRoute enables you to easily provide domain and network administrators proof of Internet connectivity problems.
  • Find out where a Web site or IP address is located! VisualRoute identifies the actual city and country location, in addition to the WHOIS location, and the network being used.
  • See where hackers are located! Identify where threats to your system and information are coming from by locating where intrusion attempts originate.
  • Fight SPAM! Quickly identify and report Spammers and Internet abusers using VisualRoute's worldwide network provider contact information.
  • Know who you are communicating with! Use VisualRoute with eMailTrackerPro to determine where the sender of an email is located.

      Roger said "Quickly locate the source of Internet "

      Wooah, for a second there I thought I would be able to locate "the source." Is this the world playing tricks on me? Where's the white rabbit? Woahh, I know Kung Fu!!!!
Give it up! You can't rely on location information...
by Anonymous Monk on Jun 21, 2004 at 08:55 UTC

    You can not get the address of a visitor reliably from a server, webhosting service, or ISP
    The closest thing you can have is the claimed address of the the proxy which the user is using to get to your site.

    The fact of the matter is that you do not get the this information. All you get is the claimed information of the IP address. For some public ISPs, you get this information from the ISP. But that requires the ISP to track this infomation. And while many ISPs and webhosting providers try to do a reasonable job, you can not rely on this effort for compliance-type policing of inappropriate content.

    The best you can do is track users and their declaration that they live in country X and not in country Y. But these declarations do little more than provide non-US residents a good chuckle every once in a while...

    "I promise I am not a Bad Foreigner and I promise I won't use this software to help people Americans don't like".
    Yeah, right. That's reliable... Whatever makes you feel good.
    ...The question begs... What is it you are publishing as ads that can't be shown in the Middle East?


Re: How to check the origin ofi a visitor?
by castaway (Parson) on Jun 21, 2004 at 22:43 UTC
    I'm using Geo::IP::PurePerl to find the origin country of an IP.. which works for around 85-90% of them. (This is a pop3-preconnect app, which deletes email based on origin ,)


[OT]Re: How to check the origin ofi a visitor?
by fraktalisman (Hermit) on Jun 21, 2004 at 17:13 UTC
    As the majority of internet users probably use Internet Explorer on Windows computers, you could try client-side JScript to determine the language: thelanguage=navigator.userLanguage as described here. (Note that this will not work for other browsers and has nothing to do with Perl.)