Search code examples
phpparsingcgiexecution

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.


Solution

  • I've tried everything and nothing seems to work properly or at all. Therefore, for me, the only answer is that it just cannot or should not be done. Besides, mixing CGI/Perl with PHP is probably not the best practice.

    It shouldn't too difficult to simply rewrite any small CGI/Perl scripts into PHP. Anything too large to re-write, then just sticking with Perl for the whole site would be best in those cases.