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

Hello, I am interested in pushing dynamic content on my website based on each visitor's general geographic location. From what I've read, if a user isn't behind a proxy server, using their IP address, I should be able to determine their approximate physical location. Unfortunately, I am no networking guru, so I was hoping that someone would be able to point me in the right direction. I know I have to access environment variables to determine the IP. But beyond this vague tidbit, I am not sure how to proceed. any help would be very welcome. thank you, Shaheeb R. www.ahmonra.com

Replies are listed 'Best First'.
Re: Geographic lookup via IP
by epoptai (Curate) on May 30, 2001 at 11:40 UTC
    IPtoLL is a hostname to latitude/longitude conversion tool (perl module) that uses whois and the DBI module to interface with a relational database (like SQL) in which a domain-to-city cache and city-to-location database are stored. Not perfect but it does work suprisingly well.

    http://www-unix.mcs.anl.gov/~olson/IPtoLL.html

    Update: You can't automatically determine the general geographic location of a visitor with greater than national resolution. What you can do is determine the address listed in the DNS record that may or may not correspond to the general location of the box the visitor is accessing your site with. Most of the time the user is in the same country the request came from, if the DNS record is correct.

    --
    Check out my Perlmonks Related Scripts like framechat, reputer, and xNN.

Re: Geographic lookup via IP
by BMaximus (Chaplain) on May 30, 2001 at 11:31 UTC
    In a perfect world. You would be able to determine a persons physical location using the IP address. Unfortunatly (or rather fortunatly) this isn't a perfect world and it's somewhat not possible. You're better off asking them and letting them volunteer that information rather than trying to do it behind their backs and violating their privacy. Be a mench and play fair.

    BMaximus

      Violating their privacy? That's kinda strong language for reading somebody's IP address off of an HTTP request, don't you think? Any time I visit merlyn's home page, I get that information thrown back at me, and don't think twice about it.

      The perception of violated privacy might get a few people antsy, I suppose, but only if you're rubbing their noses in it. I'd be much more worried about the technical aspects of it, and it sounds like an acceptable solution exists (provided it's not mission-critical. I can't imagine how it possibly could be mission critical, of course, but somebody had to say it...).



      If God had meant us to fly, he would *never* have give us the railroads.
          --Michael Flanders

        What you may think is strong I might think is just right for my cup of tea. If someone came looking for you that you didn't know. Wouldn't you feel threatened? If you don't then you must not care. I do. I'd rather they ask me where I came from and if I felt it necessary to tell them I would, but I want that choice left up to me to decide. If its general then I don't care. If my IP number says I live in the U.S. then that's fine. Its not enough information for a person to realy go by. But if I can be tracked to my front door by my IP number, well gee I sure don't want some stranger coming up to my home with hostile intentions. So you may find my words strong, but at least you know my position. I'm the guy waving the flag that says "Don't Tread On Me".

        BMaximus
        update:-2 :( the price for being honest and expressing my opinion.
Re: Geographic lookup via IP
by DBX (Pilgrim) on May 30, 2001 at 10:15 UTC
    IP is found in $ENV{REMOTE_ADDR}. To see what else you have available, I suggest:
    use CGI; my $q = new CGI; print $q->header; for my $k (sort keys %ENV){ print "$k=$ENV{$k}<BR>\n"; }
    Can't really help much with the geographical stuff.
Re: Geographic lookup via IP
by tune (Curate) on May 30, 2001 at 18:36 UTC
    NetGeo also worth a check. They have a web interface, and downloadable libraries (Perl module too).
    I am using whois servers for the same purpose.
    BTW, I asked a similar question some months ago here.

    --
    tune

Re: Geographic lookup via IP
by tomhukins (Curate) on May 30, 2001 at 18:34 UTC
Re: Geographic lookup via IP
by petdance (Parson) on May 30, 2001 at 23:17 UTC
    Don't count on it, unless you're going to think that every AOL.com visitor is coming from Dulles, VA.

    xoxo,
    Andy

    %_=split/;/,".;;n;u;e;ot;t;her;c; ".   #   Andy Lester
    'Perl ;@; a;a;j;m;er;y;t;p;n;d;s;o;'.  #   http://petdance.com
    "hack";print map delete$_{$_},split//,q<   andy@petdance.com   >