Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-17-2012, 03:36 PM   PM User | #1
WebLizzard
New Coder

 
Join Date: Aug 2009
Location: Maryland
Posts: 45
Thanks: 20
Thanked 0 Times in 0 Posts
WebLizzard is an unknown quantity at this point
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..
WebLizzard is offline   Reply With Quote
Old 09-17-2012, 03:46 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
WebLizzard (09-17-2012)
Old 09-17-2012, 03:50 PM   PM User | #3
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,496
Thanks: 44
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Users who have thanked tangoforce for this post:
WebLizzard (09-17-2012)
Old 09-17-2012, 04:22 PM   PM User | #4
WebLizzard
New Coder

 
Join Date: Aug 2009
Location: Maryland
Posts: 45
Thanks: 20
Thanked 0 Times in 0 Posts
WebLizzard is an unknown quantity at this point
Thanks so much guys! I was able to fix it - I appreciate the help and info!!!!
~Liz
WebLizzard is offline   Reply With Quote
Old 09-17-2012, 11:54 PM   PM User | #5
nevenne
New to the CF scene

 
Join Date: Sep 2012
Location: serbia
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
nevenne is an unknown quantity at this point
<?php
ob_start();
echo '<meta http-equiv="refresh" content="1;http://www.yourdomain.com/" />';
ob_flush();
?>
nevenne is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:44 PM.


Advertisement
Log in to turn off these ads.