LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   run CGI and pass GET parameters (https://www.linuxquestions.org/questions/programming-9/run-cgi-and-pass-get-parameters-675819/)

extasic 10-12-2008 07:07 AM

run CGI and pass GET parameters
 
Hi,

I want to include the output of the mailgraph perl script (mailgraph.cgi) in my php web. The graphics I need are generated by passing a "QUERY_STRING" to the script, so I would get the output by typing

Code:

export QUERY_STRING="0-n"
./mailgraph.cgi

in the shell.

That graphics should be included in a web that is used by several users simultaneously so I'm looking for a shell command that is executed within PHP and passes a QUERY_STRING value without using the environment variable so the different users don't get in each others way.

Do you know how I can solve that?

Thank you in advance!

rglang 10-16-2008 08:17 AM

You can do a couple of things. The most straightforward is to pass the data in as arguments to the command itself:

./mailgraph.cgi?0-n

This is easily handled by the script and this form is typically used when passing parameters from a Web page (like a URL link) to a CGI program.

Rich

extasic 10-16-2008 11:06 AM

thank you for your answer, but that seems not to work (or I am doing something wrong):

Code:

mail:/usr/lib/cgi-bin# ./mailgraph.cgi?n-0
bash: ./mailgraph.cgi?n-0: No such file or directory
mail:/usr/lib/cgi-bin# perl mailgraph.cgi?n-0
Can't open perl script "mailgraph.cgi?n-0": No such file or directory
mail:/usr/lib/cgi-bin#


keefaz 10-16-2008 11:50 AM

I would try:
PHP Code:

putenv('QUERY_STRING="0-n"');
echo 
system('/path/to/mailgraph.cgi'); 

or:
PHP Code:

echo system('export QUERY_STRING="0-n"; /path/to/mailgraph.cgi'); 


extasic 10-16-2008 01:22 PM

that is what I tried first, but what happens if two users use this at the same time? the export will then be overwritten by the second, won't it?

keefaz 10-17-2008 08:05 AM

I don't think so, AFAIK the variables are exported to the same or child process but not to another process

rglang 10-17-2008 08:53 AM

When CGI scripts are executed from a Web server like Apache, each "instance" of the script is executed within the context of a newly-created shell. Each shell has its own environment, which will be unique from all other instances. Therefore, you have nothing to fear - multiple users will be properly handled.

The solution I presented will work as well - you have to escape the special meaning of the "?" character when you try to run your command at the prompt:

mail:/usr/lib/cgi-bin# ./mailgraph.cgi\?n-0

You cgi script will have to parse the argument string properly, but if you've been parsing GET input from web forms, the code should already be in place to do that.

Rich


All times are GMT -5. The time now is 05:23 PM.