tags:

views:

45

answers:

4

I have a CGI based form that I am working with and I would like to be able to include some PHP code in the generated results. However, when I do this, the PHP does not appear to get processed and just ends up being displayed in the resulting web page.

Is what I am doing actually possible or am I missing something?

Thanks.

+1  A: 

No, that's not possible. You could however, in a PHP script, include('http://url.to/cgi/script'); and then point your browser at the PHP script rather than the CGI script. This will cause PHP to open a new connection to the server, execute the CGI script, and then parse it's output as if it were a PHP script.

EDIT2: Here's how you could do it with postdata including file uploads:

// Edit to match your CGI URI:
$url_to_cgi = "http://{$_SERVER['SERVER_NAME']}/cgi-bin/something.cgi";

$curl = curl_init($url_to_cgi);
curl_setopt($curl,CURLOPT_POST, true);
curl_setopt($curl,CURLOPT_RETURNTRANSFER, true);

// Pass POSTDATA along to the CGI script:
$postdata = $_POST;

// If we have file uploads, pass those along too:
if(is_array($_FILES)) foreach($_FILES as $key => $file)
  $post[$key] = "@{$file['tmp_name']}";

curl_setopt($curl,CURLOPT_POSTFIELDS, $postdata);

$file = tempnam('/tmp','php-curl-');
file_put_contente($file, curl_exec($curl);

include($file);

unlink($file);

Please note that's untested...

Josh
This gets me most of the way there. Is there a simple way to pass on data from a form POST to the above method?
Joe Corkery
@Joe: OK. Now that will be a lot more tricky. You'll need to generate a full fledged request with something like [PHP's curl functions](http://us.php.net/manual/en/function.curl-exec.php), and then `include` the result. If it could fit into the query string, that would work...
Josh
@Joe: Please see my edited answer
Josh
@Josh: I had to make some edits to the code (missing params to curl_setop) and a few missing parentheses, but otherwise that accomplished 95% of what I wanted to do. Thanks! The only problem I am seeing now is that I have a file input which doesn't seem to be getting passed along with the POST info. Any thoughts on why that might be a problem?
Joe Corkery
@Joe: Sorry, I see the errors you were talking about and that was pretty sloppy. I'll update my answer, and will include a file upload.
Josh
@Joe: Please check my updated answer v2.5, now including file uploads!
Josh
@Josh: Thanks. I'll give it a shot.
Joe Corkery
@Josh: The newest changes accomplish the task. Thanks for helping me out here.
Joe Corkery
@Joe: Glad I could help!
Josh
+1  A: 

Make sure the PHP code is being put in the page before it hits the PHP parser.

I've had success using PHP to execute a CGI script and output that to the page, but you generally have to evaluate the returned code or something along those lines.

You may be able to do some trickery by including the CGI script from the PHP. Assuming the PHP runtime will accept that and your script returns valid PHP code, that should do the trick (the script will be run and the code returned before the page is fully parsed, so your code should be processed correctly).

There may be better official ways to handle it, but those have worked for me.

peachykeen
+2  A: 

No. PHP is a server-side language. PHP code must be executed on the server. If you include it in your output then the code is sent over your TCP connection to the user's web browser.

Instead of printing the PHP, try invoking the php executable, such as via system("php myscript.php").

Beware that PHP likes to display a full set of HTTP headers in its output, so you may not get quite what you're looking for if you execute it in the middle of your CGI output.

John Kugelman
A: 

Eventually. Apache2 has something called filters. mod_ext_filter might be able to get the CGI output and pass it through PHP again.

However, I'm not sure this is what you want. And if, how to configure it. But just have a look here: http://httpd.apache.org/docs/2.2/filter.html

mario