[FAQ] Why am I getting the error message 'Headers already sent'?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Philip Ronan

    [FAQ] Why am I getting the error message 'Headers already sent'?

    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 started sending out the content of the page.

    Web content is always delivered with a few headers at the top, ending with a
    blank line. For example, a web page might start like this:
    [color=blue]
    > HTTP/1.1 200 OK
    > Date: Tue, 01 Mar 2005 12:00:00 GMT
    > Content-Type: text/html
    >
    > <html>
    > <body> ... etc...[/color]

    Once you have started sending the content of a document, you can't add any
    more headers because they won't appear at the top.

    To get round this problem, use PHP's output control functions to buffer the
    output before it is sent. For example, you can generate a "Content-Length"
    header as follows:
    [color=blue]
    > <?php
    >
    > ob_start(); // Turn on output buffering
    >
    > // Create your web page/jpeg file/whatever here
    > echo "<html><bod y> ... ";
    >
    > // Generate a "Content-Length" header
    > $clen = ob_get_length() ;
    > header("Content-Length: $clen");
    >
    > // Now send the buffered content
    > ob_flush();
    >
    > ?>[/color]

    --
    phil [dot] ronan @ virgin [dot] net



  • Chung Leong

    #2
    Re: [FAQ] Why am I getting the error message 'Headers already sent'?

    A seconary question (off the main list perhaps?)
    -----------------------------------------------------

    Q. After turning on output buffering I am still getting 'Headers already
    sent.' What the?
    A. Something, somewhere is sent to the browser prior to the call to
    ob_start().

    One possible culprit is white-spaces contained in an included file. To fix
    this, move the call to ob_start() ahead of any include/require statements.

    Another possible culprit is UTF-8 encoding. Unicode-capable editor often
    place an invisible character at the beginning of a UTF-8 text file to mark
    it as UTF-8. This character will be output before any PHP statements are
    executed. To fix this, resave the file as ASCII.


    Comment

    • Jan Pieter Kunst

      #3
      Re: [FAQ] Why am I getting the error message 'Headers already sent'?

      Chung Leong wrote:[color=blue]
      > A seconary question (off the main list perhaps?)
      > -----------------------------------------------------
      >
      > Q. After turning on output buffering I am still getting 'Headers already
      > sent.' What the?
      > A. Something, somewhere is sent to the browser prior to the call to
      > ob_start().
      >
      > One possible culprit is white-spaces contained in an included file. To fix
      > this, move the call to ob_start() ahead of any include/require statements.
      >
      > Another possible culprit is UTF-8 encoding. Unicode-capable editor often
      > place an invisible character at the beginning of a UTF-8 text file to mark
      > it as UTF-8. This character will be output before any PHP statements are
      > executed. To fix this, resave the file as ASCII.[/color]

      Wouldn't that be throwing the baby out with the bathwater? If I remember
      correctly, the invisible character in question is the 'BOM' (Byte Order
      Mark), and it's usually an option in Unicode-aware editors to save a
      file with or without BOM.

      I would advise to try saving without BOM first and only resort to ASCII
      if that fails.

      JP

      --
      Sorry, <devnull@cauce. org> is a spam trap.
      Real e-mail address unavailable. 5000+ spams per month.

      Comment

      • Philip Ronan

        #4
        Re: [FAQ] Why am I getting the error message 'Headers alreadysent'?

        Chung Leong wrote:
        [color=blue]
        > A seconary question (off the main list perhaps?)
        > -----------------------------------------------------
        >
        > Q. After turning on output buffering I am still getting 'Headers already
        > sent.' What the?
        > A. Something, somewhere is sent to the browser prior to the call to
        > ob_start().
        >
        > One possible culprit is white-spaces contained in an included file. To fix
        > this, move the call to ob_start() ahead of any include/require statements.
        >
        > Another possible culprit is UTF-8 encoding. Unicode-capable editor often
        > place an invisible character at the beginning of a UTF-8 text file to mark
        > it as UTF-8. This character will be output before any PHP statements are
        > executed. To fix this, resave the file as ASCII.[/color]

        Good point :-)

        I also made a mistake in my example script: "ob_flush() " should be changed
        to "ob_end_flush() ".

        --
        phil [dot] ronan @ virgin [dot] net



        Comment

        • Chung Leong

          #5
          Re: [FAQ] Why am I getting the error message 'Headers already sent'?


          "Jan Pieter Kunst" <devnull@cauce. org> wrote in message
          news:42255c0b$0 $28987$e4fe514c @news.xs4all.nl ...[color=blue]
          > Wouldn't that be throwing the baby out with the bathwater? If I remember
          > correctly, the invisible character in question is the 'BOM' (Byte Order
          > Mark), and it's usually an option in Unicode-aware editors to save a
          > file with or without BOM.
          >
          > I would advise to try saving without BOM first and only resort to ASCII
          > if that fails.[/color]

          Consider the audience though. The likely scenario is someone editting in
          Notepad and accidently saving the file as UTF-8.

          The character in question is the zero-width non-breaking space (U+FFEF). In
          UTF-16 text it's used as a byte order indicator. In UTF-8 it's just a
          signature. If you leave it out then an editor might not be able to correctly
          sniff out the encoding, leaving to other problems.


          Comment

          • Daniel Tryba

            #6
            Re: [FAQ] Why am I getting the error message 'Headers already sent'?

            Philip Ronan <invalid@invali d.invalid> wrote:[color=blue]
            > Q. Why am I getting the error message 'Headers already sent'?[/color]
            [ A. the explanation why and how to counter the symptoms]

            If you get this error your script flow is broken (in most cases). Using
            OB is nothing more than hiding the symptoms of the error.

            The error tells you where the real problem lies:
            1:<?php
            2:error_reporti ng(E_ALL);
            3://do stuff
            4:echo "redirectin g";
            5:
            6://do more stuff
            7:
            8:header("Locat ion: http://localhost/");
            9:?>

            will produce the error:

            Warning: Cannot modify header information - headers already sent by
            (output started at /path/to/script.php:4) in /path/to/script.php on line 8

            What is really is trying to say:

            Error: line 8 at /path/to/script.php can't send headers. The problem is
            at line 4 in /path/to/script.php, it produced some output to the client
            so I already had sent all headers before getting to line 8.

            But since you already have all required userinput you should find out
            wheter you need to sent additional headers (like a redirect) before
            outputting anything to the client. So the equivalent script with correct
            top down flow would be:
            1:<?php
            2:error_reporti ng(E_ALL);
            3:if($condition ){
            4:header("Locat ion: http://localhost/");
            5:die("redirect ing");
            6:}
            7:
            8:echo "condition was false";
            9:?>


            The following script:
            1:<?php
            2://do stuff
            3:?>
            4:<html><body>
            5:Redirecting
            6:</body></html>
            7:<?php
            8:header("Locat ion: http://tmp.tryba.nl/");
            9:?>

            Produces:

            Warning: Cannot modify header information - headers already sent by
            (output started at /path/to/script.php:7) in /path/to/script.php on line 8

            Now it complains about line 7 since the <?php line 7 apparently flushed
            the stringbuffer that got build between lines 3 and 7.

            When including file the error could look like this:

            Warning: Cannot modify header information - headers already sent by
            (output started at /path/to/other.php:5) in /path/to/script.php on line 6

            Now it is other.php which produces output to the browser at line 5, so
            line 6 in script.php can't modify headers any longer.

            Comment

            Working...