如何在 CGI 脚本中访问请求的 HTTP 标头?
我在小型应用程序和测试代码中使用过一点 Perl,但我对网络和 CGI 还很陌生。
我了解如何制作请求的标头(使用 CGI.pm 并打印 header() 函数的结果),但无法找到有关如何访问发送到我的 CGI 脚本的标头的任何信息。有人能指出我正确的方向吗?
这可能来自这样的请求:
curl http://127.0.0.1:80/cgi-bin/headers.cgi -H "HeaderAttribute: value"
I've used Perl a bit for small applications and test code, but I'm new to networking and CGI.
I get how to make the header of a request (using CGI.pm and printing the results of the header() function), but haven't been able to find any info on how to access the headers being sent to my CGI script. Could someone point me in the right direction?
This could be from a request like this:
curl http://127.0.0.1:80/cgi-bin/headers.cgi -H "HeaderAttribute: value"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
CGI 模块有一个
http()
函数,您可以使用它来实现此目的:尝试一下;以上给了我:
The CGI module has a
http()
function you can use to that purpose:Try it out; the above gives me:
除了 CGI.pm
http()
方法可以从环境变量中获取 HTTP headers 信息。因此,如果您使用类似 CGI::Minimal ,它没有http方法。你可以这样做:
In addition to the CGI.pm
http()
method you can get HTTP headers information from the environment variables.So in case you are using something like CGI::Minimal, which doesn't have the http method. you can do something like:
它们作为环境变量提供,例如
您可能需要做一些事情来配置您的 Web 服务器以提供这样的变量。
They're supplied as environment variables, such as
You may have to do something to configure your web server to supply such a variable, though.