doubi8965 2010-06-04 12:57
浏览 45
已采纳

禁止从Ruby运行cgi PHP的PHP头文件

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

"
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 " 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!

  • 写回答

1条回答 默认 最新

  • duande9301 2010-06-04 14:41
    关注

    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
    
    "
    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 />"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?