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.
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...
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.
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.
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