Forum Moderators: coopster & phranque

Message Too Old, No Replies

Detecting IP Addresses Most accurately with a script

dealing with pesky proxies

         

JerryOdom

5:06 pm on Nov 5, 2004 (gmt 0)

10+ Year Member



Hello all,

I'm tying to determine the correct order to assign an IP address to a variable so that proxies are handled correctly and the proper ip is identified. Based on the environment variables available.

The code thus far looks like this and I suppose I want to know if the elsif conditions are in the right order as I'm a bit hazy on how the whole thing works exactly. I suspect the $ENV{REMOTE_ADDR} check may need to be taken last.

Thoughts?

if($ENV{REMOTE_ADDR} && $ENV{REMOTE_ADDR} is valid){ $ourip = $ENV{REMOTE_ADDR} }
elsif($ENV{HTTP_X_FORWARDED_FOR} && $ENV{HTTP_X_FORWARDED_FOR} is valid){ $ourip = $ENV{HTTP_X_FORWARDED_FOR} }
elsif($ENV{HTTP_VIA} && $ENV{HTTP_VIA} is valid){ $ourip = $ENV{HTTP_VIA} }
elsif($ENV{HTTP_X_COMING_FROM} && $ENV{HTTP_X_COMING_FROM} is valid){ $ourip = $ENV{HTTP_X_COMING_FROM} }
elsif($ENV{HTTP_X_FORWARDED} && $ENV{HTTP_X_FORWARDED} is valid){ $ourip = $ENV{HTTP_X_FORWARDED} }
elsif($ENV{HTTP_FORWARDED} && $ENV{HTTP_FORWARDED} is valid){ $ourip = $ENV{HTTP_FORWARDED} }
elsif($ENV{HTTP_COMING_FROM} && $ENV{HTTP_COMING_FROM} is valid){ $sendip = $ENV{HTTP_COMING_FROM} }
elsif($ENV{HTTP_FORWARDED_FOR} && $ENV{HTTP_FORWARDED_FOR} is valid){ $sendip = $ENV{HTTP_FORWARDED_FOR} }

Dreamquick

5:46 pm on Nov 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The only variable you can trust is REMOTE_ADDR as the rest are unauthenticated and are under the control of the user/client, which means they have the potential to be inaccurate data - as long as you bear that in mind when you're dealing with the problem you shouldn't go too far wrong.

- Tony