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

Re: $ENV{REMOTE_ADDR} problem

by harangzsolt33 (Chaplain)
on Oct 24, 2018 at 06:31 UTC ( [id://1224583]=note: print w/replies, xml ) Need Help??


in reply to $ENV{REMOTE_ADDR} problem

I usually do this:
my $IP = ENV('REMOTE_ADDR', '0.0.0.0'); print $IP; exit; ################### # This function returns an environment variable named in the # first argument. If the environment variable doesn't exist, # then DEFAULT is returned. However, if a third argument is # given, then that will be returned in every case. # The third argument is used for tweaking and testing purposes. # # Usage: VALUE = ENV( NAME, [DEFAULT, [OVERRIDE]] ) # sub ENV { return '' unless @_; # No arguments at all? my $NAME = shift; # Get NAME my $DEFAULT = @_ ? shift : ''; # Get DEFAULT value return shift if @_; # OVERRIDE? return $DEFAULT unless defined $NAME; # NAME undefined? return $DEFAULT unless $NAME; # NAME is blank? if (exists($ENV{$NAME})) { return Trim($ENV{$NAME}); } return $DEFAULT; } ################### # # This function removes whitespace before and after STRING # and returns a new string. # # Usage: STRING = Trim(STRING) # sub Trim { return '' unless @_; my $T = shift; return '' unless defined $T; return '' unless length($T); my $s = -1; # start ptr my $e = 0; # end ptr for (my $i = 0; $i < length($T); $i++) { if (vec($T, $i, 8) > 32) { if ($s < 0) { $s = $i; } $e = $i; } } return substr($T, $s, $e - $s + 1); }
This means, even if REMOTE_ADDR is undefined for whatever reason, I get a string in $IP which is going to be 0.0.0.0. And then I know that it's because it was undefined!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-03-29 11:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found