Treating CGI output as PHP?

Need help installing PHP, configuring a script, or configuring a server? Then come on in and post your questions! We'll try to help the best we can!

Moderator: General Moderators

Post Reply
skylark2
Forum Commoner
Posts: 32
Joined: Wed Jun 16, 2010 6:00 am

Treating CGI output as PHP?

Post by skylark2 »

Longstanding programmer here, but very new to web development...I'm not sure whether I'm asking this is the right section, as most of my confusion is because I'm not sure where to look for an answer...

What I have is an old, old program which we want to convert to a web application without having to rewrite all the computation. So, executable is compiled and renamed to .cgi, and I also have a menu + forms in php format. User clicks on the menu item, sees the correct form, fills it in, values are passed to the CGI executable, calculations are done, CGI executable writes to standard output and this comes up in the browser. All this works fine.

My problem is that the CGI output goes straight to the browser as-is. I'd like it to be treated as php, so I can, for instance, include my menu at the top with <?php include("index.php"); ?> so the user can go on and do something else. What I can't figure out is how to do that. Everything I've found is geared towards changing how different filetypes are processed...but I don't have a filename with an extension, just the CGI output.

If I save the output from my CGI program as a file with a .php extension and reload it into the browser, I get my menu as expected, so it isn't an error in the output.

Apache 2.2.15 (Win32) PHP/5.3.2, if it matters.

This has to be easy, or at least possible, right?

Many thanks for any assistance.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Treating CGI output as PHP?

Post by AbraCadaver »

If your forms are posting straight to the executable, then I would do this:

1. Change the forms to post to a PHP script
2. In the PHP script pass off the posted data via POST to the executable and capture the response:

Code: Select all

$message = http_parse_message(http_post_fields('http://www.example.com/cgi-bin/executable.cgi', $_POST));
$output = $message->body;
Then use $output however you wish.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
skylark2
Forum Commoner
Posts: 32
Joined: Wed Jun 16, 2010 6:00 am

Re: Treating CGI output as PHP?

Post by skylark2 »

Thanks! That sounds like exactly what I need.
skylark2
Forum Commoner
Posts: 32
Joined: Wed Jun 16, 2010 6:00 am

Re: Treating CGI output as PHP?

Post by skylark2 »

Sadly, I'm completely unable to install php_http to get the functions to work :( No matching Windows binaries right now, and I've found a few people saying it doesn't work with Apache 5.3.

Methodology seems sound, though. I guess I'll just have to pass the arguments in and out the hard way (pulls out php manual and starts flicking).

Many thanks for the help. I'm in that frustrating state where the knee bone's connected to the ankle bone and I keep finding something else I have to do before I can do any real work...

Edit: doesn't work with PHP 5.3. Brain fritz.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Treating CGI output as PHP?

Post by AbraCadaver »

This should do it:

Code: Select all

function http_post_lite($url, $data, $headers=null) {
	
	$data = http_build_query($data);	
	$opts = array('http' => array('method' => 'POST', 'content' => $data));
	
	if($headers) {
		$opts['http']['header'] = $headers;
	}
	$st = stream_context_create($opts);
	$fp = fopen($url, 'rb', false, $st);
	
	if(!$fp) {
		return false;
	}
	return stream_get_contents($fp);
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply