如何在 Apache2 CGI 中捕获参数值
我在 Ubuntu 上有一个 apache2 CGI 应用程序。 CGI 处理程序是 bash shell 脚本。
我的客户端应用程序是 search.html:
<html>
<body>
<form action="/cgi-bin/search.sh" method="post">
<input type="text" name="searchKey" size="10"></input>
<input type=SUBMIT value="search">
<form>
</body>
</html>
首先,我只想捕获服务器端“searchKey”参数的值。我尝试了以下操作,但没有显示任何内容。
search.sh 是:
#!/bin/bash
echo Content-type:text/plain
echo ""
echo $SEARCHKEY
伙计们,你能告诉我如何在服务器端捕获参数的值吗?
更新
谢谢您的所有回答。我知道要获取发布请求的值需要从 STDIN 读取数据。
我尝试按照 Ithcy 的建议,如下
#!/bin/bash
echo post=$(</dev/stdin)
echo 'content length:'$CONTENT_LENGTH
echo 'content:'$post
它只显示:
content length:30
content:
为什么内容什么都没有?我需要在 Apache 服务器上进行更多配置才能读取发布数据吗? 谢谢
I have a little apache2 CGI application on the Ubuntu. The CGI handler is bash shell script.
My client application is search.html:
<html>
<body>
<form action="/cgi-bin/search.sh" method="post">
<input type="text" name="searchKey" size="10"></input>
<input type=SUBMIT value="search">
<form>
</body>
</html>
firstly, I just want to catch value of "searchKey" parameter in server side. I tried like following, but displaying nothing.
search.sh is:
#!/bin/bash
echo Content-type:text/plain
echo ""
echo $SEARCHKEY
Guys, can you tell me how to catch value of the parameter in the server side?
UPDATE
thank you for all answers.I understood that to get a value of post request need to read data from STDIN.
i tried as Ithcy suggest like following
#!/bin/bash
echo post=$(</dev/stdin)
echo 'content length:'$CONTENT_LENGTH
echo 'content:'$post
it was displaying only that:
content length:30
content:
why is content nothing? do i need to do more configure on Apache server to read post data?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
POST 将通过 STDIN 发送。
但如果可以的话,你确实应该考虑使用 perl(或 python、PHP 等),正如 Glenn Jackman 所建议的那样。
POSTs will come through STDIN.
But you really should look at using perl (or python, PHP, etc) if you can, as Glenn Jackman suggests.
整个查询字符串由
$QUERY_STRING
变量表示。您可以通过在 shell 脚本中运行不带参数的env
来查看这一点。仅获取 searchKey 值的示例:
更新:抱歉,这只适用于您使用 GET 发布表单的情况。我没有阅读详细信息=/
如果您确实需要阅读帖子,此页面可能会帮助您:http://digitalmechanic.wordpress.com/2008/02/21/handling-post-data-in-bash-cgi-scripts/
但我没有让它发挥作用。
The whole querystring is represented in the
$QUERY_STRING
variable. You can see this by runningenv
without arguments in your shell script.Example for getting only the searchKey value:
Update: I'm sorry, this only applies if you are using GET to post your form. I didn't read the details =/
If you really need to read POSTs, this page may help you: http://digitalmechanic.wordpress.com/2008/02/21/handling-post-data-in-bash-cgi-scripts/
I didn't get it to work, though.
抱歉,这几个月没有人回答你的问题。这是可行的:
您必须在 /bin/bash 之后插入一个空行(如果没有 echo,则 printf "\n" 即可)
Sorry no one answered your question all these months. This works:
You must insert a blank line after /bin/bash (if not echo, printf "\n" will do)
这是关于 CGI 协议的很好的文档: http://hoohoo.ncsa.illinois.edu/cgi/
我建议您考虑使用具有良好 CGI 库的语言(例如 Perl),这样您就不必重新发明几年前已经完善的轮子。
This is good documentation about the CGI protocol: http://hoohoo.ncsa.illinois.edu/cgi/
I'd suggest you consider using a language (such as Perl) with a good CGI library so you don't have to reinvent a wheel that's been perfected years ago.
尝试
代替
Try
instead of
尝试使用此脚本来列出您输入的内容:
Try this script to list content of your input: