How to fetch HTTP headers in perl when using CGI

妖精的绣舞 提交于 2019-11-28 07:38:30

问题


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


回答1:


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}.




回答2:


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...



来源:https://stackoverflow.com/questions/4949276/how-to-fetch-http-headers-in-perl-when-using-cgi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!