[KLUG Members] Getting X-Forwarded-For

Adam Tauno Williams adam at morrison-ind.com
Fri Jan 6 10:01:33 EST 2006


When an HTTP client traverses a proxy the proxy is supposed to add the
X-Forwarded-For (I have "forwarded_for on" in my squid.conf).  So in PHP
code I want to grab the value of this header,  but for the life of me I
can't find the thing.

$headers = apache_request_headers();
if (array_key_exists('X-Forwarded-For', $headers)) {
  printf("Ah ha!  You crazy cat, you\'re using a proxy server!\n");
  printf("Your host\'s IP address is %s\n",
$headers["X-Forwarded-For"]);
 } else {
     printf("Oh, my!  Aren't we a slacker,  not bothering to use a proxy
server.\n");
     printf("Your host's IP address is: %s\n", $_SERVER['REMOTE_ADDR']);
    }

<aside>Googling on this pretty much just finds a bunch of twits who want
to appear smart so they are going on and on about how you can't trust
HTTP headers and not to use them for authentication, blah, blah, blah
(pretty much all copy-n-pasting something they read somewhere);
sometimes Google is very frustrating.</aside>

The above code always says a proxy is not in use although it merrily
returns the IP address of the proxy.  Anyone know if this is a squid
thing, and apache thing, or a PHP thing?
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
Url : http://www.kalamazoolinux.org/pipermail/members/attachments/20060106/21cb1ad2/attachment.bin


More information about the Members mailing list