禁止从 Ruby 运行 cgi PHP 的 PHP 标头

发布于 2024-09-04 02:02:23 字数 1121 浏览 3 评论 0原文

诚然,这对我来说是一个奇怪的问题,但这就是我正在做的事情:

我有一个 Ruby 脚本,它正在执行一串 PHP 代码并捕获输出。

这与我遇到的另一个问题 从命令行运行 cgi PHP。

这是 Ruby 脚本的源代码:

#!/usr/bin/ruby
puts "Content-type: text/html\n\n"
puts "Start PHP Output<br />"
puts `echo '<?php echo "hello world"; ?>' | php5 -q`
puts "End PHP Output<br />"

我无法弄清楚的真正奇怪的事情是,当我从命令行运行 Ruby 脚本与从 CGI 运行 Ruby 脚本时,这里的 PHP 代码似乎表现不同。这对我来说确实没有意义,因为无论哪种方式,我都会使用相同的参数从命令执行相同的 PHP 字符串。

当我从命令行运行上述 Ruby 脚本时,我得到了预期的输出:

内容类型:text/html

开始 PHP 输出
hello world 结束 PHP 输出

当我通过 CGI 从浏览器点击相同的 Ruby 脚本时,我得到以下输出:

启动 PHP 输出
X-Powered-By: PHP/5.2.13 内容类型:text/html
放置“内容类型:text/html\n\n” 放置“启动 PHP 输出
回显“你好世界”| php5 -q 放置 “结束 PHP 输出
”结束 PHP 输出

在我看来,正在发生的事情是 PHP 字符串没有抑制标头,就像我期望 -q 选项所做的那样......并且还将我的整个 Ruby 脚本转储回浏览器 - 这让我感到困惑。

有什么想法吗?

提前致谢!

Admittedly, this is a strange problem for me to have, but here is what I'm doing:

I've got a Ruby script that is executing a string of PHP code and capturing the output.

This is somewhat related to another problem that I had with running cgi PHP from the command line.

Here is the Ruby script's source:

#!/usr/bin/ruby
puts "Content-type: text/html\n\n"
puts "Start PHP Output<br />"
puts `echo '<?php echo "hello world"; ?>' | php5 -q`
puts "End PHP Output<br />"

The really odd thing that I can't figure out is that the PHP code here seems to behave differently when I run the Ruby script from the command line vs. from CGI. Which really doesn't make sense to me, because either way I'm executing the same string of PHP from the command with the same arguments.

When I run the above Ruby script from the command line, I get the output that I expect:

Content-type: text/html

Start PHP Output
hello world End
PHP Output

When I hit that same Ruby script from a browser via CGI, I get this output:

Start PHP Output
X-Powered-By:
PHP/5.2.13 Content-type: text/html
puts "Content-type: text/html\n\n"
puts "Start PHP Output
" puts
echo 'hello world' | php5 -q puts
"End PHP Output
" End PHP
Output

It looks, to me, like what is happening is that the string of PHP is not suppressing the headers, like I would expect the -q option to do... and is also dumping my entire Ruby script back to the browser - which baffles me.

Any thoughts?

Thanks in advance!

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

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

发布评论

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

评论(1

那请放手 2024-09-11 02:02:23

据我所知,PHP 接管了整个请求和处理过程。使用所要求的相同文件重新运行它,然后在调用时将控制权返回给 ruby​​。如果可能的话,如果您想用它来做的话,请使用 CLI 而不是 CGI。

您可以尝试清除 cgi 确定其位于网络服务器请求中的环境变量。

#!/usr/bin/ruby
puts "Content-type: text/plain\n\n"
puts "Start PHP Output<br />"
f = IO.popen("env -i php5-cgi -q","r+")
f.write("<?php var_dump(time());?>");
f.close_write()
f.each {|line| puts line}
f.close()
puts "End PHP Output<br />"

As far as I can tell, PHP takes over the entire request & reruns it with the same file asked for, after which it returns control to ruby at the point of invocation. Please us the CLI instead of the CGI if possible if this is what you're trying to do with it.

You could try to clear up the environmental variables by which the cgi determines it's in a webserver request.

#!/usr/bin/ruby
puts "Content-type: text/plain\n\n"
puts "Start PHP Output<br />"
f = IO.popen("env -i php5-cgi -q","r+")
f.write("<?php var_dump(time());?>");
f.close_write()
f.each {|line| puts line}
f.close()
puts "End PHP Output<br />"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文