NYCPHP Meetup

NYPHP.org

[nycphp-talk] headers already sent...

Chris Shiflett shiflett at php.net
Mon Apr 19 18:41:23 EDT 2004


--- Greg Faber <greg at click3x.com> wrote:
> Warning: Cannot send session cache limiter - headers already sent 
> (output started at /var/www/xtranet-staging/upload.php:1) in 
> /var/www/xtranet-staging/upload.php on line 3

I think these errors are pretty clear, but just to paraphrase, this tells
you that you have output on line 1 of upload.php. Later, on line 3, you
try to start a session, the the session cache limiter cannot be sent
(because it consists of HTTP headers, and you have previous output).

> Warning: Cannot add header information - headers already sent by 
> (output started at /var/www/xtranet-staging/upload.php:1) in 
> /var/www/xtranet-staging/upload.php on line 91

This is an almost identical warning. The same output on line 1 that caused
the headers to be sent is a problem when you get to line 91 where you are
apparently using the header() function to try to add your own. Once your
script outputs anything, the headers are sent, and you can no longer add
headers like this. PHP is just letting you know that this header cannot be
added, and it continues on.

> Basically I know why I get these warnings, ie: I'm sending headers 
> twice

The best way to make a problem difficult to solve is to make an erroneous
assumption. :-)

> header("location:upload.php")

While you're at it, you may want to fix your Location header to be of a
compliant format:

header('Location: http://example.org/path/to/script.php');

> What I don't get is why are headers being sent without my knowledge?

Headers are sent for every page. How would you like to be notified? :-)

Just kidding. You can check to see if headers have been sent with the
headers_sent() function, but there's no way to try to send them yourself.
It's an implicit operation.

There are basically two steps involved with the content generation phase,
which is where PHP operates. PHP first has the ability to manipulate
headers (whether through the header() function or as a side-effect of
something else). It then gets to send the content. As soon as you begin to
generate content, PHP says, "Oh, I guess you're done manipulating headers.
Hey Apache, here is my list of header modifications. I'll be sending
content now."

> This works fine just running off my home computer so what's the deal?

You may have output buffering enabled for every script on your home
computer. This causes all output to be buffered and only really output at
the very end of the script (or when it is flushed with a function,
whichever comes first).

I guess the confusing thing is really that header() doesn't affect when
headers are sent, which is what many people seem to think.

Hope that helps.

Chris

=====
Chris Shiflett - http://shiflett.org/

PHP Security - O'Reilly
     Coming Fall 2004
HTTP Developer's Handbook - Sams
     http://httphandbook.org/
PHP Community Site
     http://phpcommunity.org/



More information about the talk mailing list