0

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!

asked Jun 4, 2010 at 12:57
2
  • 1
    This might be better suited to ServerFault. It might be something like the configuration in CGI is different than the configuration for your user. Commented Jun 4, 2010 at 13:30
  • Thanks, I've posted this at ServerFault: serverfault.com/questions/148205/… Commented Jun 4, 2010 at 14:29

1 Answer 1

1

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 />"
answered Jun 4, 2010 at 14:41
Sign up to request clarification or add additional context in comments.

1 Comment

It is not possible (this is currently running on a hosted machine) for me to use CLI over CGI - that was the first thing I looked into. It appears as though adding the "env -i" DOES fix the issue that I was having! The simplicity is genius! Thanks a lot!!!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.