+ Reply to Thread
Results 1 to 9 of 9

Thread: header function fails even on pure php pages

  1. #1
    geirgrusom is offline x10Hosting Member geirgrusom is an unknown quantity at this point
    Join Date
    Mar 2009
    Posts
    9

    header function fails even on pure php pages

    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. #2
    lemon-tree's Avatar
    lemon-tree is offline x10 Minion lemon-tree has a spectacular aura about
    Join Date
    Nov 2007
    Posts
    1,420

    Re: header function fails even on pure php pages

    Are you sure it is not trying to send an output to the browser?

  3. #3
    blobtech is offline x10Hosting Member blobtech is an unknown quantity at this point
    Join Date
    Nov 2008
    Location
    Netherlands
    Posts
    16

    Re: header function fails even on pure php pages

    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 by blobtech; 03-31-2009 at 04:31 PM.

  4. #4
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: header function fails even on pure php pages

    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 by garrettroyce; 03-31-2009 at 04:52 PM.
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  5. #5
    geirgrusom is offline x10Hosting Member geirgrusom is an unknown quantity at this point
    Join Date
    Mar 2009
    Posts
    9

    Re: header function fails even on pure php pages

    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 by geirgrusom; 04-01-2009 at 07:43 AM. Reason: Figured it out

  6. #6
    smooth2 is offline x10Hosting Member smooth2 is an unknown quantity at this point
    Join Date
    Apr 2009
    Posts
    1

    Re: header function fails even on pure php pages

    thats right the same old BOM
    use notepad++ (use unix format )

  7. #7
    geirgrusom is offline x10Hosting Member geirgrusom is an unknown quantity at this point
    Join Date
    Mar 2009
    Posts
    9

    Re: header function fails even on pure php pages

    I use Visual Studio, so I fixed it by choosing Advanced Save Options->Use UTF8 without byte order mark.

  8. #8
    taekwondokid42 is offline x10 Lieutenant taekwondokid42 is an unknown quantity at this point
    Join Date
    Aug 2007
    Posts
    268

    Re: header function fails even on pure php pages

    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. #9
    dickey's Avatar
    dickey is offline x10 Sophmore dickey is an unknown quantity at this point
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    128

    Re: header function fails even on pure php pages

    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.
    Don't get me wrong as I believe if and when I help someone I also help myself whereby whatever someone learns I also learn.

    But I will also accept credits or reps if you really want to part with it.

+ Reply to Thread

Similar Threads

  1. tons of PHP Resources
    By Chris S in forum Scripts & 3rd Party Apps
    Replies: 10
    Last Post: 01-16-2009, 10:07 AM
  2. how can i convert Php pages and code into asp pages and code
    By n4tec in forum Scripts & 3rd Party Apps
    Replies: 11
    Last Post: 01-10-2009, 09:40 PM
  3. [PHP] PHP For Starters
    By Complex in forum Tutorials
    Replies: 24
    Last Post: 06-14-2008, 11:40 PM
  4. [PHP] Placing the Ads in PHP Pages
    By Chris in forum Tutorials
    Replies: 22
    Last Post: 06-04-2005, 11:14 PM
  5. PHP Pages Help
    By Akkarin in forum Scripts & 3rd Party Apps
    Replies: 2
    Last Post: 06-02-2005, 04:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers