"Cannot modify headers information" error
Results 1 to 14 of 14

Thread: "Cannot modify headers information" error

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    14

    "Cannot modify headers information" error

    Having thought I'd got this issue fixed and the code working nicely, I'm getting this error:

    Warning: Cannot modify header information - headers already sent ... on line 43
    My code is as follows (needless to say I can't see the problem, but I'm sure it's an obvious solution that I'm missing):

    PHP Code:
    <?php

    /* Subject and e-mail variables */

        
    $emailSubject 'Request a Call';
        
    $webMaster 'enquiries@mydomain.co.uk';
        
    $formSuccess "http://www.mydomain.co.uk/form-success.html";
        
    $formError "http://www.mydomain.co.uk/form-error.html";
        
    /* Gathering data variables */

        
    $titleField $_POST['title'];
        
    $firstnameField $_POST['first_name'];
        
    $surnameField $_POST['surname'];
        
    $daytimetelField $_POST['daytime_tel'];
        
    $eveningtelField $_POST['evening_tel'];
        
    $marketingsourceField $_POST['marketing_source'];
        
    $termsandconditionsField $_POST['terms_and_conditions'];
        
    /* Anti-Spam */

    if ( preg_match"/[\r\n]/"$firstnameField ) || preg_match"/[\r\n]/"$surnameField ) ) {
        
    header"Location: $formError);
        exit ;
    }

    /* Message to WebMaster */

        
    $body = <<<EOD
    <br><hr><br>
    <b>Full Name:</b> <br>
    $titleField $firstnameField $surnameField <br><br>
    <b>Daytime Telephone:</b> <br>
    $daytimetelField <br><br>
    <b>Evening Telephone:</b> <br>
    $eveningtelField <br><br>
    <b>How did you hear about us?</b> <br>
    $marketingsourceField <br><br>
    <b>Accepts T&Cs:</b> <br>
    $termsandconditionsField <br><br>
    EOD;

        
    $headers "Content-type: text/html\r\n";
        
    $success mail($webMaster$emailSubject$body$headers);
        
    /* Successful Submission Redirect */

    header"Location: $formSuccess);
    exit ;

    ?>
    Any help would be most gratefully received.

    Many thanks,

    Max
    Last edited by bradgrafelman; 05-06-2013 at 06:03 PM. Reason: bbcode tags modified

  2. #2
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,413
    You've cut off the second most important part of the error message. PHP will tell you exactly where output began.

    Also, when posting PHP code, please use the board's [php]..[/php] bbcode tags as they make your code much easier to read and analyze.

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    14
    Ah sorry, here's a fuller version of the error:

    Warning: Cannot modify header information - headers already sent by (output started at /websites/123reg/LinuxPackage22/[...]mysiteurl.co.uk/public_html/formprocess_request-a-call.php:1) in /websites/123reg/LinuxPackage22/[...]mysiteurl.co.uk/public_html/formprocess_request-a-call.php on line 43
    Does that provide the information required?

    Thanks for your help.

    Max

  4. #4
    PHP Witch laserlight's Avatar
    Join Date
    Apr 2003
    Location
    Singapore
    Posts
    13,560
    Looking at "formprocess_request-a-call.php:1", I would guess that you have a blank line at the top that shouldn't be there.
    Use Bazaar for your version control system
    Read the PHP Spellbook
    Learn How To Ask Questions The Smart Way

  5. #5
    Junior Member
    Join Date
    May 2013
    Posts
    14
    Quote Originally Posted by laserlight View Post
    Looking at "formprocess_request-a-call.php:1", I would guess that you have a blank line at the top that shouldn't be there.
    As per the PHP coding, sadly it's not that simple. There's no space, line or anything there.

  6. #6
    PHP Witch laserlight's Avatar
    Join Date
    Apr 2003
    Location
    Singapore
    Posts
    13,560
    Ahh... check that you don't have a Unicode byte order mark (BOM) there. This can be done with certain editors, or by using a hex editor or hex dump.
    Use Bazaar for your version control system
    Read the PHP Spellbook
    Learn How To Ask Questions The Smart Way

  7. #7
    Junior Member
    Join Date
    May 2013
    Posts
    14
    Hi laserlight,

    Thanks for your reply.

    Unfortunately I must confess I'm a beginner to PHP and don't quite understand what you're suggesting or how to check. Would you be able to provide any further details for me please?

    Best wishes,

    Max

  8. #8
    Junior Member
    Join Date
    May 2013
    Posts
    14
    Update: I managed to find a few articles on BOMs and how to detect them. I've saved the files without the BOMs and so far so good! I'll let you know if I have any further problems, but thanks so much - I'd never have found the solution without you!

    Max

  9. #9
    Senior Member
    Join Date
    Apr 2003
    Location
    Silver Lake
    Posts
    4,851
    OK that sounds like it was fairly tricky (and props to laserlight and Mad Max for working it out). Max, can you tell us what editor you were using? Also, I find it curious that PHP wouldn't know how to distinguish a BOM from actual PHP code and/or output.
    IMPORTANT: STOP using the mysql extension. Use mysqli or pdo instead.
    World War One happened 100 years ago. Visit Old Grey Horror for the agony and irony.

  10. #10
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,862
    Also, I find it curious that PHP wouldn't know how to distinguish a BOM from actual PHP code and/or output.
    There's no way it could: just because a file starts with \xEF\xBB\xBF doesn't mean it's a UTF-8-encoded text file (which - in a sensible world - would be a sign that it had been converted from UCS16-LE or UCS16-BE which use the Byte Order Mark to distinguish themselves from each other). Some brain-dead editors use \xEF\xBB\xBF to say "hey, this is UTF-8 encoded!" but that's not what it's for.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  11. #11
    Senior Member
    Join Date
    Apr 2003
    Location
    Silver Lake
    Posts
    4,851
    Quote Originally Posted by Weedpacket View Post
    There's no way it could
    Welllllll....not saying this is how things should be (and they probably shouldn't be), but it would seem that certain text editors seem to avoid putting those bytes at the beginning of one's edit window when encountering a BOM at the beginning of a text file and additionally make the assumption that the bytes that follow represent UTF-8 text (or something). While it is entirely possible that someone might make a poorly designed PHP file that starts with these bytes:
    PHP Code:
    \xEF\xBB\xBF are the bytes that represent a BOM which in some cases...
    <?php
    /**
     * some-fing-template.php
     */
    ?>
    the odds of this happening seem fairly remote. I find it difficult to imagine any other circumstances under which a PHP file would start with a BOM. I expect I'm headed for some more mind-hurting realizations thanks to Weedpacket's generous donation of know-how here.

    It seems odd to me that there aren't distinct file suffixes for UTF8 text files and ISO text files and LATIN-1 text files and so on. On the other hand, from a PHP perspective, I seem to recall that the entirety of PHP's syntax is expressed in ASCII characters and therefor any byte sequences intended as UTF-8 which happen to live in a PHP file would either have to be a) output living outside the <?php ?> tags or b) somewhere in a quoted string in which case PHP would treat them as precisely the bytes that they are without any care in the world as to that they represent.
    IMPORTANT: STOP using the mysql extension. Use mysqli or pdo instead.
    World War One happened 100 years ago. Visit Old Grey Horror for the agony and irony.

  12. #12
    Junior Member
    Join Date
    May 2013
    Posts
    14
    Quote Originally Posted by sneakyimp View Post
    Max, can you tell us what editor you were using?
    I usually use Dreamweaver, but due to the wonders of 123-reg, I had recently resorted to using Notepad... which was where it all went wrong. With Dreamweaver, when you Save As, it offers a handy little tick box to say if you want BOMs included or not - that's how I fixed it.

  13. #13
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,862
    Quote Originally Posted by sneakyimp
    I seem to recall that the entirety of PHP's syntax is expressed in ASCII characters and therefor any byte sequences intended as UTF-8 which happen to live in a PHP file would either have to be a) output living outside the <?php ?> tags or b) somewhere in a quoted string in which case PHP would treat them as precisely the bytes that they are without any care in the world as to that they represent.
    Nope; you can have non-ASCII characters in identifiers (you can have a variable called $résumé).

    a) output living outside the <?php ?> tags
    Which is exactly where the BOM - when it's present - is found.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  14. #14
    Senior Member
    Join Date
    Mar 2009
    Posts
    802
    Quote Originally Posted by Mad Max View Post
    I usually use Dreamweaver, but due to the wonders of 123-reg, I had recently resorted to using Notepad... which was where it all went wrong. With Dreamweaver, when you Save As, it offers a handy little tick box to say if you want BOMs included or not - that's how I fixed it.
    Oh man, Notepad...

    Try out Komodo Edit. The best editor I've ever used. I've converted my entire office and we were a mixture of Dreamweaver, Eclipse, Notepad2, and Notepad++. No one can imagine going back.
    Declare variables, not war.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •