Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't include/execute a CGI file inside a PHP file

Using SSI, I can simply include this in my HTML file and the output of the CGI script is displayed on the page:

<!--#include virtual="script.cgi"-->

Now I'm trying to do the equivalent, but instead of a HTML file, it's inside a PHP file. I've been researching this all day and every solution is giving me the following problems. I'll list them each starting with the ones I thought should work as well as the obscure...


1) <?php include("script.cgi"); ?>

Result: No execution. This simply prints out the full content of the CGI file as if it was just a text file.


2) <?php virtual("script.cgi"); ?>

Result: Fatal error: Call to undefined function virtual() in test.php on line #.

This bothers me as everything I read indicates that this is the proper way to do it. I'm no expert in PHP but how can virtual be an "undefined function"? It's listed in the official PHP docs as valid for PHP version 5.


3) <?php exec("script.cgi"); ?>

Result: Blank. Nothing.


4)

<?php
    $fd = fopen("script.cgi", "r");
    fpassthru($fd);
?>

Result: No execution. This simply prints out the full content of the CGI file as if it was just a text file.


5) <?php echo system("script.cgi"); ?>

Result: Mixed. It executes but the "result" of the CGI script is printed twice and it's preceded by "Content-type: text/html". This I don't understand.

Content-type: text/html resultresult

And by removing the echo...

<?php system("script.cgi"); ?>, results in...

Content-type: text/html result


6) <?php print("/usr/bin/perl script.cgi"); ?>

Result: No execution. It simply prints out what's contained after the print statement (/usr/bin/perl script.cgi). (I'm not surprised but somebody in a forum claimed this worked for them.)


So what do I need to do in order to execute and include the results of the CGI file? This seems like it should be easy but I don't know what else to try.

I'm on an Apache/Linux server (cPanel) with access to edit the htaccess file. It's running PHP version 5.2.17.

And yes, when I pull up the CGI file in my browser, it executes and displays the result, so we know the server is configured to execute CGI files.

like image 233
Sparky Avatar asked Mar 04 '26 04:03

Sparky


1 Answers

If PHP runs as an Apache module, CGI variables are passed internally. Before calling a CGI script, which expects to find them in the environment, one should copy them there.

Then, the CGI script will likely write the HTTP response header before the content. There doesn't seem to be a way to prevent PHP from displaying a header. So the script has to handle the script output appropriately.

For example:

<?php
    foreach($_SERVER as $key => $value)
        putenv("$key=$value");
    $rtc = 0;
    $out = array();
    $mycgi = "/full/path/to/my/script.cgi";
    exec($mycgi, $out, $rtc);
    $in_header = true;
    foreach($out as $value)
    {
        if ($in_header)
        {
            if (strlen($value) == 0)
                $in_header = false;
            else
                header($value);
        }
        else
        {
            echo "$value\n";
        }
    }
    if ($rtc)
        error_log("$mycgi rtc=$rtc", 0);
?>
like image 87
Ale Avatar answered Mar 05 '26 19:03

Ale



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!