Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Retrieving the REMOTE HOST

by Anonymous Monk
on Jul 07, 2007 at 23:23 UTC ( #625459=perlquestion: print w/replies, xml ) Need Help??

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

I was wondering if retrieving the remote host depends on your server?

In a PHP script, it can retrieve the REMOTE_HOST easily, but when I write this following perl script to see all the environmental variables my shared host can grab, it SKIPS the REMOTE_HOST value.
#!/usr/local/bin/perl -T use strict; print "Content-type: text/html\n\n"; print "<tt>\n"; foreach my $key (sort keys(%ENV)) { print "$key = $ENV{$key}<p>"; } print $ENV{'REMOTE_HOST'}; #prints BLANK for me?! print "<br>hi";
Is it something to do with my code or the server settings? If server settings, how can I work around this if my host does not cooperate?

Thank you monks.

Replies are listed 'Best First'.
Re: Retrieving the REMOTE HOST
by snopal (Pilgrim) on Jul 08, 2007 at 00:28 UTC

    This usually does the trick for me:

    my $address = $ENV{X_FORWARDED_FOR} || $ENV{REMOTE_ADDR} || "";

    This returns an IP or a zero length string, which tends to be accurate enough for use as a factor in authentication. You can not be assured that the values provided by the web server environment variables are not spoofed, so multi-factoring of your incoming information is useful.

Re: Retrieving the REMOTE HOST
by varian (Chaplain) on Jul 08, 2007 at 12:46 UTC
    This behavior is dictated by the CGI specifications.

    The CGI environment variable specifications document that the variable REMOTE_HOST does not always get set. Typically this can happen for local requests on ip 127.0.0.1.

    You probably want to use REMOTE_ADDR as a more reliable reference. It returns an IP address though.

Re: Retrieving the REMOTE HOST
by naikonta (Curate) on Jul 08, 2007 at 13:34 UTC
    I rarely use this information in my program for serious purposes so I just stuff with REMOTE_HOST || REMOTE_ADDR, which will give me at least the IP address. If the request came by proxy than I would get that proxy IP address instead. I know there exists HTTP headers to distinguish these proxies in between, but I'll always have to lookup the references over the Internet to recall again about HTTP_VIA and HTTP_X_FORWARDED_FOR.

    Here's a page discussing various proxy types and how they fill in those related HTTP headers. It would tell you right away your IP address, whether you're coming by a proxy, and if so, the proxy's IP address as well. You seem to have really consider it if you need accurate remote host or IP address information.


    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Re: Retrieving the REMOTE HOST
by jasonk (Parson) on Jul 09, 2007 at 00:04 UTC

    Many web servers are configured not to resolve the IP addresses of clients connecting (Apache for example, is configured this way by default) simply because most people don't ever use that information, and doing a DNS request for every connection to your web server to get information you aren't going to use anyway is somewhat wasteful. In this situation, the best thing for you to do is to get $REMOTE_ADDR instead, and resolve it yourself if you really need a hostname instead of an IP address.


    We're not surrounded, we're in a target-rich environment!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://625459]
Approved by GrandFather
Front-paged by derby
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2021-06-24 16:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (130 votes). Check out past polls.

    Notices?