473,383 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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

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
content,
exactly the thing I need to avoid.

I've tried:
while ( @ob_end_flush() );
print('');
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
anything
for how to MAKE the headers go. How do I do it? PHP 4.3.11.

Thanks,
-Chap

Oct 3 '05 #1
6 1879
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.

Oct 4 '05 #2
Joshie Surber wrote:
i don't see what your problem is here -- you are printing something on
the page, right?


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
task
turn out to be the only way possible in PHP? Have I missed anything in
the docs?

Oct 4 '05 #3
> exec /replaces/ PHP with the filter, so if PHP hasn't written the headers
by then, it never will.

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.

ECRIA
http://www.ecria.com
Oct 4 '05 #4
I wrote:
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.
ECRIA Public Mail Buffer wrote: 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().


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

Oct 6 '05 #5
> "exec /replaces/ PHP with the filter, so if PHP hasn't written the headers
by then, it never will." This is a DIRECT QUOTE. You seem to have meant pcntl_exec.

The context: You had said, immediately prior to this *fallacious
assertion* - "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."
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.

ECRIA
http://www.ecria.com
Oct 6 '05 #6
The "ECRIA Public Mail Buffer" quotes me, directly, verbatim,
and out of context (the second time in a row):
"exec /replaces/ PHP with the filter, so if PHP hasn't This is a DIRECT QUOTE.


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
pcntl_exec:
"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


and then educates me about my nerve and my grip:
But whatever the case may be - what a nerve you do have! You had
better get a grip and quench your misguided frustration


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.

Oct 6 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bob | last post by:
Seem to have a problem ending a session. I get the following message. Warning: session_start(): Cannot send session cookie - headers already sent by (output started at...
5
by: Philip Ronan | last post by:
OK, here's my 2p worth: === Q. Why am I getting the error message 'Headers already sent'? A. PHP produces this error message when you try to set a header for a web page after you have already...
10
by: Lisa Pearlson | last post by:
Hi, I have a php script with no more than this: <?php echo "Hello World!"; ?> When a webbrowser client requests data, it receives Apache server headers, followed by my data: HTTP/1.1 200...
1
by: Mike P | last post by:
I'm using MamboServer for my site, and have switched from SEF URLs to standard URLs. Of course, I'm indexed everywhere with the SEF urls. I'm trying to write a script, so that users and search...
8
by: Andreas Klemt | last post by:
Hello, I get this error Message "cannot redirect after http headers have been sent" when I do this response.redirect ("home.aspx") How can I find out with vb.net if already a http header has...
3
by: Reporter | last post by:
Here is an example from the PHP Manual <?php if ((!isset($_SERVER)) || (1==1)) { header('WWW-Authenticate: Basic realm="My Realm"'); header('HTTP/1.0 401 Unauthorized'); echo 'Text to send...
4
by: craigtomo | last post by:
I am getting the following error when i try to log on to my data base Warning: session_register() : Cannot send session cookie - headers already sent by (output started at...
2
by: urbanedge | last post by:
I've uploaded a site to a new server and now the headers in the mail function are not working to send the required email response. This is a newly acquired site and my php knowledge is at the...
6
Markus
by: Markus | last post by:
Things to discuss: Headers What are they? What does PHP have to do with headers? Why can they only be sent before any output? Common causes
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.