如何在 Apache2 CGI 中捕获参数值

发布于 2024-08-17 14:45:36 字数 941 浏览 2 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

述情 2024-08-24 14:45:36

POST 将通过 STDIN 发送。

#!/bin/bash
POST=$(</dev/stdin)
echo $POST

但如果可以的话,你确实应该考虑使用 perl(或 python、PHP 等),正如 Glenn Jackman 所建议的那样。

POSTs will come through STDIN.

#!/bin/bash
POST=$(</dev/stdin)
echo $POST

But you really should look at using perl (or python, PHP, etc) if you can, as Glenn Jackman suggests.

墨离汐 2024-08-24 14:45:36

整个查询字符串由 $QUERY_STRING 变量表示。您可以通过在 shell 脚本中运行不带参数的 env 来查看这一点。

仅获取 searchKey 值的示例:

echo $QUERY_STRING | sed 's/searchKey\=\([^&]\+\).*/\1/'

更新:抱歉,这只适用于您使用 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 running env without arguments in your shell script.

Example for getting only the searchKey value:

echo $QUERY_STRING | sed 's/searchKey\=\([^&]\+\).*/\1/'

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.

·深蓝 2024-08-24 14:45:36

抱歉,这几个月没有人回答你的问题。这是可行的:

#!/bin/bash
echo
echo post=$(</dev/stdin)
echo 'content length:'$CONTENT_LENGTH
echo 'content:'$post

您必须在 /bin/bash 之后插入一个空行(如果没有 echo,则 printf "\n" 即可)

Sorry no one answered your question all these months. This works:

#!/bin/bash
echo
echo post=$(</dev/stdin)
echo 'content length:'$CONTENT_LENGTH
echo 'content:'$post

You must insert a blank line after /bin/bash (if not echo, printf "\n" will do)

夜司空 2024-08-24 14:45:36

这是关于 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.

素罗衫 2024-08-24 14:45:36

尝试

echo $1

代替

echo $SEARCHKEY

Try

echo $1

instead of

echo $SEARCHKEY
心清如水 2024-08-24 14:45:36

尝试使用此脚本来列出您输入的内容:

#!/bin/bash
echo 'content length:'$CONTENT_LENGTH
read StringInBox
echo $StringInBox

Try this script to list content of your input:

#!/bin/bash
echo 'content length:'$CONTENT_LENGTH
read StringInBox
echo $StringInBox
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文