header function fails even on pure php pages

Discussion in 'Scripts, 3rd Party Apps, and Programming' started by geirgrusom, Mar 31, 2009.

  1. geirgrusom

    geirgrusom New Member

    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    Suddenly, today (or at least today I noticed it), the header functions always returns

    Warning: Cannot modify header information - headers already sent by (output started at /home/me/somefile_php:1) in /home/me/some_file.php on line x

    Why has this suddenly started? the page in question is a pure PHP page, and there is no HTML generated by it. It is just supposed to redirect to the source page after data has been recorded in the database.

    Also, this renders my captcha system useless which encounters the same error when trying to call
    header("Content-type: image/png");

    Anyone had a similar problem?
     
  2. lemon-tree

    lemon-tree x10 Minion Community Support

    Messages:
    1,420
    Likes Received:
    46
    Trophy Points:
    48
    Are you sure it is not trying to send an output to the browser?
     
  3. blobtech

    blobtech New Member Prime Account

    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    1
    Its probally because a empty line or a space just infront of the <?php tag.

    Make sure there is absolutely *no* text nor whitespace which is not included within a php tag before the header() functions is called.

    Also check any included files, they might contain that one byte that is messing it up!
     
    Last edited: Mar 31, 2009
  4. garrettroyce

    garrettroyce Community Support Community Support

    Messages:
    4,726
    Likes Received:
    162
    Trophy Points:
    63
    blobtech's answer fixes about 99.9% of these occurrences. If you intentionally want to have output before your PHP code you can use ob_start() and ob_flush() functions. But, ob_start() has to be called before any output to the browser.

    example:
    Code:
    <?php ob_start(); ?>
    <!DOCTYPE ...
    <?php //OH NO! I changed my mind! I want to redirect!
    header("Location: ...");
    ob_flush();
    Anything that has been buffered by the ob_start() function, will be overwritten if you ask it to before you call ob_flush().
     
    Last edited: Mar 31, 2009
  5. geirgrusom

    geirgrusom New Member

    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for replying :)
    But there really is nothing before <?php
    Not a single space or line break, at least thats what my text editor tells me.
    Could it be related to the UTF-8 BOM character?

    edit: turned out it was the BOM character, Thanks for replying :)
     
    Last edited: Apr 1, 2009
  6. smooth2

    smooth2 New Member

    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    thats right the same old BOM
    use notepad++ (use unix format )
     
  7. geirgrusom

    geirgrusom New Member

    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    I use Visual Studio, so I fixed it by choosing Advanced Save Options->Use UTF8 without byte order mark.
     
  8. taekwondokid42

    taekwondokid42 New Member

    Messages:
    268
    Likes Received:
    0
    Trophy Points:
    0
    I think your problem is that you are sending output to the page, and then you are trying to modify the headers (set a cookie, start a session, ect.).

    This is illegal code:


    <?

    echo "hello";
    setcookie(...);

    ?>


    Instead, this will not be illegal:

    <?

    setcookie(...);
    echo

    ?>
     
  9. dickey

    dickey New Member

    Messages:
    128
    Likes Received:
    0
    Trophy Points:
    0
    I'll go with smooth2 it is your Character encoding that is the problem...

    try to match your php file's encoding with it's with the head declaration.
     

Share This Page