Want headers sent NOW. How to make PHP send them?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Ok, I've registered all the right headers to send, with
header( 'Content-Type: .../...') etc.

The next thing to do is exec a program that will be producing the
actual content.  The content MUST begin immediately after the headers
(that is, after the single empty line that separates the headers from
the body).

PHP won't seem to send the headers unless I tell it to print something.
I tried printing an empty string  print('');  -  still doesn't work.
I can't go printing a NON-empty string - that would corrupt the
exactly the thing I need to avoid.

I've tried:
  while ( @ob_end_flush() );
  while ( @ob_end_flush() );

Still no good. Headers don't get sent.

The manual is very helpful about how to AVOID getting the headers sent
before you want them. Ok, NOW I want them. I haven't stumbled on
for how to MAKE the headers go. How do I do it?  PHP 4.3.11.


Re: Want headers sent NOW. How to make PHP send them?

i don't see what your problem is here -- you are printing something on
the page, right? otherwise it seems your app doesn't do much...

just make sure your content is set up right before you print it and i
don't see what the problem is... but if there is one that's that
because there is no way you can send headers without content, unless
the client sends a HEAD request rather than GET or POST. Sorry.

Re: Want headers sent NOW. How to make PHP send them?

Joshie Surber wrote:
Quoted text here. Click to load it

The point was, the content is produced by an external program. The PHP
script needs to output the headers and then exec the program to
produce the content. The program is a filter that reads from a file
PHP has already opened and writes content to stdout. (I should mention
this hosting service runs PHP in CGI mode.) In another language, the
right way to do it would be to exec(2) - pcntl_exec in PHP speak - the
filter, with the open file dup'd on its standard input. exec /replaces/
PHP with the filter, so if PHP hasn't written the headers by then, it
never will. Another approach that's almost as good would be popen; the
PHP script would have to spoon-feed the file contents down the filter's
input pipe, but the output goes to stdout. Won't work in PHP either,
without some way to let PHP know it needs to spit out the headers
because output is about to happen.

proc_open could be used, if there were a way to associate file
descriptor 0 with the current fd of an already open file. But if
there is, it isn't documented. Is there a way?

I got something working with passthru, which doesn't have any way to
inherit an open descriptor, so the filter had to be rewritten to take
the file name as an argument, properly escaped of course, and then
reopen it, which only works because the input in this case does happen
to be a regular file, and still creates an unavoidable race condition.
Why does the least efficient and least secure way of doing a simple
turn out to be the only way possible in PHP? Have I missed anything in
the docs?

Re: Want headers sent NOW. How to make PHP send them?

Quoted text here. Click to load it
A fallacious assertion. Once your command is run, your PHP script proceeds  
with execution. It does not die as a result of the call to exec().

Even if your program produces no specific output string, headers will be  
sent. All you can do is control *when* the headers get sent, by printing (or  
echoing) a string. If you don't do this, the headers are sent after the last  
line of your script.


Re: Want headers sent NOW. How to make PHP send them?

I wrote:
Quoted text here. Click to load it

ECRIA Public Mail Buffer wrote:
Quoted text here. Click to load it

You might look again at what I wrote. You'll see I was
describing what would be the obvious, efficient solution in a
language other than PHP, involving the POSIX exec(2), which is
not what PHP calls "exec" but rather what PHP calls "pcntl_exec."
If you are not familiar with the effects of pcntl_exec, you will
be able to find it in section CVI of the PHP manual.

The point of my post was to find out if there is any way in PHP
to implement the same efficient solution, without an extra fork
and without a race condition. It still appears that there isn't,
but if the extra process creation is tolerable, it turns out the
race condition can be avoided by using an undocumented feature of
proc_open, found in the source ext/standard/exec.c and now noted
on the proc_open manual page.

I understand how you might have thought I was off the mark if
you read the posting too quickly and thought I was talking about
what PHP calls "exec." By and large, it never hurts to give
something a closer second reading, if you find yourself about
to call it "fallacious."

Re: Want headers sent NOW. How to make PHP send them?

Quoted text here. Click to load it
This is a DIRECT QUOTE. You seem to have meant pcntl_exec.

The context: You had said, immediately prior to this *fallacious  
assertion* -
Quoted text here. Click to load it
dup'd on its standard input."
Since both pcntl_exec() and exec() are PHP functions, it is quite normal to  
expect that if you meant to describe the behavior of pcntl_exec() you would  
use the name "pcntl_exec" and not "exec" after you had established this  
context! It is your wording is that is incorrect.

But whatever the case may be - what a nerve you do have! You had better get  
a grip and quench your misguided frustration if you expect anyone here to  
assist you. One would think that you were paying us to provide these  
insights! Get over yourself - if we knew everything we would not need to  
post here.


Re: Want headers sent NOW. How to make PHP send them?

The "ECRIA Public Mail Buffer" quotes me, directly, verbatim,
and out of context (the second time in a row):

Quoted text here. Click to load it

and /then/, curiously, repeats exactly the words I had included
to set the context and make it clear that I was using "exec"
in its usual, POSIX sense - exec(2) - which PHP happens to call

Quoted text here. Click to load it

and then educates me about my nerve and my grip:

Quoted text here. Click to load it

What I get for having a conversation with a buffer, I suppose. :)
Anyway, I did get useful information elsewhere, learn a few things
about PHP I hadn't known, and get the program working, so it's all
good.  Have a nice day.

Site Timeline