Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Aug 2009
    Location
    Maryland
    Posts
    47
    Thanks
    21
    Thanked 0 Times in 0 Posts

    Question "Warning - Cannot Modify Header Information" errors

    Hi,

    I am trying to put a form on my client's website. http://www.sleightfarm.com It asks for name, address, city, etc. I got everything to work nicely except I am now getting an error message when we are hitting the "Send" button instead of the form re-directing to our thank you page. (Not to mention our error page is not showing up either.)

    Error Message I am getting when hitting send---> "Warning: Cannot modify header information - headers already sent by (output started at /home/content/30/9210630/html/send_mail.php:89) in /home/content/30/9210630/html/send_mail.php on line 95"

    I have my form on my index.html page, a thank_you.html page, a error_message.html page and a send_mail.php. I've gone into the PHP file to line 95 and this seems to be the error spot
    PHP Code:
    header("Location:$thankyou_page"); 
    It's one of the last lines of code in the PHP file. Line 89 has code about echo
    PHP Code:
     echo "Sent to $sEmailAddress"

    I've been reading up on this and people mention something about deleting white space or the header information needing to come before something? I am completely new when it comes to this stuff, so I'm sort of lost on what to change or move around. Can anyone point me in the right direction or tell me what I need to change around in my send_mail.php file? Thank you in advance for any help. These forums have been lifesavers for me!!!


    PHP Code:
    <?php

    /*
    The following function checks for email injection.
    Specifically, it checks for carriage returns - typically used by spammers to inject a CC list.
    */
    function isInjected($str) {
        
    $injections = array('(\n+)',
        
    '(\r+)',
        
    '(\t+)',
        
    '(%0A+)',
        
    '(%0D+)',
        
    '(%08+)',
        
    '(%09+)'
        
    );
        
    $inject join('|'$injections);
        
    $inject "/$inject/i";
        if(
    preg_match($inject,$str)) {
            return 
    true;
        }
        else {
            return 
    false;
        }
    }


    /*
    This first bit sets the email address that you want the form to be submitted to.
    You will need to change this value to a valid email address that you can access.
    */

    $aEmailTo = array(
        
    "screaminglizzard@gmail.com",
        
    "liz@wisewebsitecreations.com"
    ); 

    /*
    This bit sets the URLs of the supporting pages.
    If you change the names of any of the pages, you will need to change the values here.
    */
    $feedback_page "index.html";
    $error_page "error_message.html";
    $thankyou_page "thank_you.html";



    /*    Try not to change below this line if possible
        -------------------------------------------------------------------- */ 
        
    /*    ERROR CHECKING AND FORM VALIDATION 
        -------------------------------------------------------------------- */
    if ($_POST && $_POST['submit_form']) { 


        
    // Clean Data and Check for Errors. 
        
    $aErrors = array(); 
        foreach(
    $_POST as $sField => $mValue){
            
    $aClean[$sField] = filter_input(INPUT_POST,$sField,FILTER_SANITIZE_STRING); 
            if(empty(
    $aClean[$sField]) || trim($aClean[$sField]) == ''$aErrors[$sField] = true// If empty or blank add an error.  
        
    }
        if(
    isInjected($aClean['email'])) $aErrors['email'] = true;
            
        
    // Count the errors and if more than 0 show error page.     
        
    if(count($aErrors)>0){ 
            
    header"Location: $error_page" );
            exit();
        }
        
        
        
    /*    OK No errors? Great... SEND THE FORM. 
            -------------------------------------------------------------------- */
        
    else {
            
            
    // Construct the email message. 
            
    $sHTML '<strong>'.$aClean['firstname'].'</strong>&nbsp;sent you the following message,<br />';
            
    $sHTML .= '<strong>Firstname:&nbsp;</strong>'.$aClean['firstname'].'<br />';
            
    $sHTML .= '<strong>Lastname:&nbsp;</strong>'.$aClean['lastname'].'<br />';    
            
    $sHTML .= '<strong>Email:&nbsp;</strong>'.$aClean['email'].'<br />';
            
    $sHTML .= '<strong>Phone:&nbsp;</strong>'.$aClean['phone'].'<br />';
            
    $sHTML .= '<strong>Address:&nbsp;</strong>'.$aClean['address'].'<br />';
            
    $sHTML .= '<strong>City:&nbsp;</strong>'.$aClean['city'].'<br />';                
            
    $sHTML .= '<strong>State:&nbsp;</strong>'.$aClean['state'].'<br />';                
            
    $sHTML .= '<strong>Zip:&nbsp;</strong>'.$aClean['zip'].'<br />';                
            
    $sHTML .= '<strong>Best Way to Contact:&nbsp;</strong>'.$aClean['contact'].'<br />';                
            
    $sHTML .= '<strong>How Did You Hear About Us?:&nbsp;</strong>'.$aClean['referral'];        

            
    // For every email address send the message.         
            
    foreach($aEmailTo as $iIndex => $sEmailAddress){
    echo 
    "Sent to $sEmailAddress";
                
    $sHeaders  'MIME-Version: 1.0' "\r\n";
                
    $sHeaders .= 'Content-type: text/html; charset=iso-8859-1' "\r\n";
                
    $sHeaders .= 'From: '.$aClean['firstname'].' <'.$aClean['email'].'>'."\r\n";
                
    mail("$sEmailAddress","Feedback Form Results",$sHTML,$sHeaders);
            }
            
    header("Location:$thankyou_page");
            exit();             
        }    



    }else{ 
    // If they didn't hit submit send them back to the feedback form. 
        
    header"Location: $feedback_page" ); 
        exit();
    }




    ?>

    ~Liz
    Last edited by WebLizzard; 09-17-2012 at 03:39 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    PHP Code:
            // For every email address send the message.         
            
    foreach($aEmailTo as $iIndex => $sEmailAddress){
    echo 
    "Sent to $sEmailAddress";
                
    $sHeaders  'MIME-Version: 1.0' "\r\n";
                
    $sHeaders .= 'Content-type: text/html; charset=iso-8859-1' "\r\n";
                
    $sHeaders .= 'From: '.$aClean['firstname'].' <'.$aClean['email'].'>'."\r\n";
                
    mail("$sEmailAddress","Feedback Form Results",$sHTML,$sHeaders);
            }
            
    header("Location:$thankyou_page"
    Sending output to the browser flushes the headers. When it does you can no longer set any headers, including cookies and session cookies.

    I haven't counted the lines to the echo here, but the lines from the echo to the header correspond with the error, and visually it appears that the echo is on line 89. Simply remove this echo. There's no reason to use it; once you hit a header redirect it will sent a 302 to the client anyway, so they will not see any output generated from the original script.

  • Users who have thanked Fou-Lu for this post:

    WebLizzard (09-17-2012)

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,325
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    For more information on how headers work and why this error comes about (and how to deal with it) see the link in my signature.

    If you really don't want to restructure your code, you can use output buffering (again see the link) to stop any output before all the headers have been sent.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    WebLizzard (09-17-2012)

  • #4
    New Coder
    Join Date
    Aug 2009
    Location
    Maryland
    Posts
    47
    Thanks
    21
    Thanked 0 Times in 0 Posts
    Thanks so much guys! I was able to fix it - I appreciate the help and info!!!!
    ~Liz

  • #5
    New to the CF scene
    Join Date
    Sep 2012
    Location
    serbia
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <?php
    ob_start();
    echo '<meta http-equiv="refresh" content="1;http://www.yourdomain.com/" />';
    ob_flush();
    ?>


  •  

    Posting Permissions

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