如何在 Perl 中读取使用 POST 发送的 URL 数据?
我正在尝试读出从页面中的表单发送到我的 Perl 脚本的 POST 数据。我用谷歌搜索并发现:
read(STDIN, $param_string, $ENV{'CONTENT_LENGTH'})
通过在正确的位置拆分它来读取整个数据字符串并将整个字符串写入 $param_string ,
Param1=Value1&Param2=Value2&Param3=Value3
我得到了必要的数据。
但我想知道为什么我的 $param_string 是空的。
当我用 GET 尝试整个事情时:
$param_string = $ENV{'QUERY_STRING'};
一切正常。有人有想法吗?
I'm trying to read out the POST-Data that was sent from a form in a page to my Perl Script. I googled and found out that:
read(STDIN, $param_string, $ENV{'CONTENT_LENGTH'})
reads out the whole Data-String with and writes the whole string to $param_string in the form of
Param1=Value1&Param2=Value2&Param3=Value3
by spliting it at the right places I get the necessary Data.
But I wonder why my $param_string is empty.
When I try the whole thing with GET:
$param_string = $ENV{'QUERY_STRING'};
everything works fine. Does anybody have an idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
绝对没有真正的理由让像你这样级别的人想要手动解析 CGI 请求。
请使用 CGI::Simple 或 CGI.pm。
CGI.pm 有很多包袱(HTML 生成、面向功能的界面),这使得 CGI::Simple 更好。
在 CPAN 上使用任何 CGI 处理模块都比尝试从头开始编写 CGI 处理代码要好。
请参阅
CGI::Simple
中的parse_query_string
用于在处理 POST 到脚本的表单时访问使用查询字符串传递的参数的方法。如果您想了解如何正确执行此操作,可以阅读任一模块的源代码。通读 CGI.pm CHANGES 文件也很有启发。
There absolutely no real reason for someone at your level to want to hand parse CGI requests.
Please use CGI::Simple or CGI.pm.
CGI.pm has a lot of baggage (HTML generation, function oriented interface) which makes CGI::Simple preferable.
Using any CGI processing module on CPAN is better than trying to write CGI processing code from scratch.
See
parse_query_string
inCGI::Simple
for a way of accessing parameters passed using the query string when processing a form that is POSTed to your script.If you want to learn how to do it right, you can read the source code of either module. Reading through the CGI.pm CHANGES file is also instructive.
如果您能够检索 GET 数据但无法检索 POST 数据,很可能您忘记将表单方法从 post 更改为 post。您可以通过在 if 语句中使用此条件来检查您的提交方法:
If you are able to retrieve GET-data but not able to retrieve POST-data, most likely you forgot to change form method from to be post. You can check your submit method by using this condition in if statement:
在 mod_perl 2 下, Apache2::Request 对我有用。
Under mod_perl 2, Apache2::Request works for me.