开发者

How to catch value of parameter in Apache2 CGI

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


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.


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.


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)


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

echo $1

instead of

echo $SEARCHKEY


Try this script to list content of your input:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜