可以将 PHP 代码嵌入到 CGI 脚本的输出中吗?

发布于 2024-09-02 21:17:52 字数 130 浏览 4 评论 0原文

我正在使用一个基于 CGI 的表单,我希望能够在生成的结果中包含一些 PHP 代码。然而,当我这样做时,PHP 似乎没有得到处理,只是最终显示在生成的网页中。

我正在做的事情实际上是可能的还是我错过了什么?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜光 2024-09-09 21:17:52

不,那不可能。但是,您可以在 PHP 脚本中 include('http://url.to/cgi/script'); 然后将浏览器指向 PHP 脚本而不是 CGI 脚本。这将导致 PHP 打开与服务器的新连接,执行 CGI 脚本,然后解析其输出,就好像它是 PHP 脚本一样。

EDIT2:以下是如何处理包括文件上传在内的后期数据:

// 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);

请注意,未经测试...

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

仲春光 2024-09-09 21:17:52

不。PHP 是一种服务器端语言。 PHP 代码必须在服务器上执行。如果将其包含在输出中,则代码将通过 TCP 连接发送到用户的 Web 浏览器。

不要打印 PHP,而是尝试调用 php 可执行文件,例如通过 system("php myscript.php")

请注意,PHP 喜欢在其输出中显示完整的 HTTP 标头集,因此如果您在 CGI 输出中间执行它,您可能无法获得所需的内容。

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.

鹊巢 2024-09-09 21:17:52

确保 PHP 代码在到达 PHP 解析器之前就已放入页面中。

我已经成功地使用 PHP 执行 CGI 脚本并将其输出到页面,但您通常必须评估返回的代码或类似的内容。

您也许可以通过包含 PHP 中的 CGI 脚本来进行一些欺骗。假设 PHP 运行时会接受这一点,并且您的脚本返回有效的 PHP 代码,那么应该可以解决问题(脚本将在页面完全解析之前运行并返回代码,因此您的代码应该得到正确处理)。

可能有更好的官方方法来处理它,但这些对我有用。

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.

小耗子 2024-09-09 21:17:52

最终。 Apache2 有一个叫做过滤器的东西。 mod_ext_filter 或许能够获取 CGI 输出并再次通过 PHP 传递它。

但是,我不确定这是否是您想要的。如果的话,如何配置。
但看看这里: http://httpd.apache.org/docs/2.2 /filter.html

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文