7

I'm using Perl/CGI/Apache and want to fetch the X-Forwarded-For HTTP header. How do I do that?

2 Answers 2

16

Except for a few headers that are handled specially, CGI stores the value of Header-Name: in the environment variable HTTP_HEADER_NAME. So, X-Forwarded-For (if present in the request) should be found in $ENV{HTTP_X_FORWARDED_FOR}.

Sign up to request clarification or add additional context in comments.

2 Comments

@cjm, thank you so much. Is there any rfc or protocol or standard defines, web server passes http header to cgi process in environment variables way with prefix HTTP_?
@hylepo, RFC 3875
5

CGI has a method for accessing HTTP request header fields, called "http", so you can say:

my $q = CGI->new()
print $q->http('X-Forwarded-For');

This works regardless whether you're running as a CGI, in fastcgi, mod_perl, etc...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.