wright67uk Posted August 15, 2012 Share Posted August 15, 2012 Hi, I have a fully funtioning form, with the exception of the re-direct at the end. I get the following error message; Warning: Cannot modify header information - headers already sent by (output started at filelocation/contact.php on line 25) Here is contact.php; <?php $to = "edward_t_wright@hotmail.co.uk"; $from = $_REQUEST['Email'] ; $name = $_REQUEST['Name'] ; //not in use, keep for other form $headers = "From: $from"; $subject = "Web Contact Data"; $fields = array(); $fields{"Name"} = "Name"; //not in use, keep for other form $fields{"Email"} = "Email"; $fields{"Phone"} = "Phone"; $fields{"Message"} = "Message"; $body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } $headers2 = "From: edward_t_wright@hotmail.co.uk"; $subject2 = "Thank you for contacting us"; $autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usualy within 24 hours."; if($from == '') {print "You have not entered an email, please go back and try again";} else { $send = mail($to, $subject, $body, $headers); $send2 = mail($from, $subject2, $autoreply, $headers2); if($send) {header( "Location: http://www.websites.cx/indextest.html" );} else {print "We encountered an error sending your mail, please notify webmaster@YourCompany.com"; } } ?> Any tips would be great? Quote Link to comment https://forums.phpfreaks.com/topic/267095-cannot-modify-header-error/ Share on other sites More sharing options...
requinix Posted August 15, 2012 Share Posted August 15, 2012 output started at filelocation/contact.php on line 25 Whatever is on line 25 is outputting something. Maybe an error message, maybe whitespace, maybe something intentional. For header() to work there must not be any output beforehand. Quote Link to comment https://forums.phpfreaks.com/topic/267095-cannot-modify-header-error/#findComment-1369509 Share on other sites More sharing options...
Christian F. Posted August 15, 2012 Share Posted August 15, 2012 Please read this thread: HEADER ERRORS - READ HERE BEFORE POSTING THEM. Quote Link to comment https://forums.phpfreaks.com/topic/267095-cannot-modify-header-error/#findComment-1369580 Share on other sites More sharing options...
wright67uk Posted August 15, 2012 Author Share Posted August 15, 2012 Ok Ive got it. No html or white space above the header. Thankyou for the replies! Ive moved the two if/else if's around; <?php $to = "edward_t_wright@hotmail.co.uk"; $from = $_REQUEST['Email'] ; $name = $_REQUEST['Name'] ; //not in use, keep for other form $headers = "From: $from"; $subject = "Web Contact Data"; $fields = array(); $fields{"Name"} = "Name"; //not in use, keep for other form $fields{"Email"} = "Email"; $fields{"Phone"} = "Phone"; $fields{"Message"} = "Message"; $body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } $headers2 = "From: edward_t_wright@hotmail.co.uk"; $subject2 = "Thank you for contacting us"; $autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usualy within 24 hours."; if($send) {header( "Location: http://www.websites.cx/indextest.html" );} else {print "We encountered an error sending your mail, please notify webmaster@YourCompany.com"; } if($from == '') {print "You have not entered an email, please go back and try again";} else { $send = mail($to, $subject, $body, $headers); $send2 = mail($from, $subject2, $autoreply, $headers2); } ?> I no longer get the header error. The problem now is that $send is empty? so I get my error message ; "We encountered an error sending your mail, please notify webmaster@YourCompany.com"; if i move $send = mail($to, $subject, $body, $headers); $send2 = mail($from, $subject2, $autoreply, $headers2); to above the if's ill get the header error again. Whats the best way to juggle this about? Quote Link to comment https://forums.phpfreaks.com/topic/267095-cannot-modify-header-error/#findComment-1369679 Share on other sites More sharing options...
xyph Posted August 15, 2012 Share Posted August 15, 2012 Give it a shot first! Keep in mind you gotta call the header BEFORE any echos or prints. Try planning it out in pseudo-code Quote Link to comment https://forums.phpfreaks.com/topic/267095-cannot-modify-header-error/#findComment-1369682 Share on other sites More sharing options...
wright67uk Posted August 15, 2012 Author Share Posted August 15, 2012 thanks for the reply. Thats what I thought I was doing but I cant figure how I would handle if($send) {header( "Location: http://www.websites.cx/indextest.html" );} if if($from == '') {print "You have not entered an email, please go back and try again";} else { $send = mail($to, $subject, $body, $headers); $send2 = mail($from, $subject2, $autoreply, $headers2); } provides the value for $send. Quote Link to comment https://forums.phpfreaks.com/topic/267095-cannot-modify-header-error/#findComment-1369697 Share on other sites More sharing options...
DavidAM Posted August 15, 2012 Share Posted August 15, 2012 The if ($send) ... needs to be inside of the else on the if ($from .... Otherwise, you are sending a message which your code says is invalid. if($from == '') { print "You have not entered an email, please go back and try again";} } else { $send = mail($to, $subject, $body, $headers); $send2 = mail($from, $subject2, $autoreply, $headers2); if($send) { header( "Location: http://www.websites.cx/indextest.html" ); exit(); ## ALWAYS INCLUDE AN EXIT AFTER A HEADER Redirect. Otherwise, the PHP engine will continue executing code. } else { print "We encountered an error sending your mail, please notify webmaster@YourCompany.com"; } } Quote Link to comment https://forums.phpfreaks.com/topic/267095-cannot-modify-header-error/#findComment-1369723 Share on other sites More sharing options...
Christian F. Posted August 15, 2012 Share Posted August 15, 2012 Why are you testing if the mail was sent successfully, before you've attempted to send it? That just doesn't make any sense. In addition to that, I've got a few other comments: Always use die (); after header ('Location: ...'), otherwise the script will continue to parse and lots of unplanned/weird things might happen. Failure to do so might also open for security risks. Please use proper indenting in your code, for your sake as well as ours. This script is wide open for any attackers to abuse, you absolutely need to validate all input from the user. Lest you want your script to be abused by spam-bots, to send spam-mails to random individuals. Yes, even if you have specified the "TO:" header; It can be overwritten. DavidAM: I know you know this, but... You do realize there is no need for that else after the exit (). Right? Quote Link to comment https://forums.phpfreaks.com/topic/267095-cannot-modify-header-error/#findComment-1369728 Share on other sites More sharing options...
DavidAM Posted August 16, 2012 Share Posted August 16, 2012 @ChristianF: Yeah. I'm going to have to modify my signature. I often copy the OP's code and make minor changes. I figure they will recognize the code easier to see where the changes need to be. Aside from moving that IF statement, the only thing I did was to add the exit call; that particular change is (or can be) critical and I thought it necessary to include it. I do not feel the need to completely rewrite the code. If the OP doesn't recognize his/her code, they may have trouble incorporating the changes or just doing a copy-paste without understanding what change they are making. Either way, it does little good for anyone (IMHO). Quote Link to comment https://forums.phpfreaks.com/topic/267095-cannot-modify-header-error/#findComment-1369785 Share on other sites More sharing options...
wright67uk Posted August 16, 2012 Author Share Posted August 16, 2012 Thankyou for the replies. By doing $send = mail($to, $subject, $body, $headers); $send2 = mail($from, $subject2, $autoreply, $headers2); if($send) { header( "Location: http://www.websites.cx/indextest.html" ); exit(); then surely im outputting html, before the header? please excuse me if im being silly. Quote Link to comment https://forums.phpfreaks.com/topic/267095-cannot-modify-header-error/#findComment-1369797 Share on other sites More sharing options...
maxudaskin Posted August 16, 2012 Share Posted August 16, 2012 Thankyou for the replies. By doing $send = mail($to, $subject, $body, $headers); $send2 = mail($from, $subject2, $autoreply, $headers2); if($send) { header( "Location: http://www.websites.cx/indextest.html" ); exit(); then surely im outputting html, before the header? please excuse me if im being silly. The best way to prevent header errors is to, instead of putting php in the middle of html code, separate your code into three main catagories Models Views Controllers Models are the liaisons between your code and databases, xml files, ect. Use functions such as get_posts, save_post, remove_post. Views are the files containing the HTML code. The only php on the page should be to output data. <h1>Posts</h1> <?php foreach($posts as $post): ?> <div> <?=$post?> </div> <?php endforeach; ?> Controllers control the data that is being sent to the views. Let's say that a user wants to view a post. Controller is activated. Controller gets the id of the post from the URI, $_GET['id'] Controller asks the Model for the post from the database. $model->get_post($id); Model runs get_post. Queries the database for a post with a 'post_id' of $id Model either returns the post data, or false if it didn't find anything Controller gets the post data. Checks if false. If false, returns a 'Post Not Found' error. Controller sends the post data to the View file. View echoes the post data. User reads the article and goes about the rest of their day, none the wiser. Here's some reading material. It is a bit lengthy. Quote Link to comment https://forums.phpfreaks.com/topic/267095-cannot-modify-header-error/#findComment-1369804 Share on other sites More sharing options...
DavidAM Posted August 16, 2012 Share Posted August 16, 2012 Thankyou for the replies. By doing $send = mail($to, $subject, $body, $headers); $send2 = mail($from, $subject2, $autoreply, $headers2); if($send) { header( "Location: http://www.websites.cx/indextest.html" ); exit(); then surely im outputting html, before the header? please excuse me if im being silly. It does kind of feel that way, doesn't it. I mean, after all, there is a print statement before the header call. But if you follow the flow of the logic; when that print statement is actually executed, you will never get to the header call. Quote Link to comment https://forums.phpfreaks.com/topic/267095-cannot-modify-header-error/#findComment-1369808 Share on other sites More sharing options...
wright67uk Posted August 16, 2012 Author Share Posted August 16, 2012 Firstly - Maxudaskin, thankyou for the explanation, I do need to set aside some time to read up on this! DavidAM - Now im really puzzled - lol The mail does send but The header error does still remain Warning: Cannot modify header information - headers already sent by ..... on line 22. (labelled) <?php $to = "edward_t_wright@hotmail.co.uk"; $from = $_REQUEST['Email'] ; $name = $_REQUEST['Name'] ; //not in use, keep for other form $headers = "From: $from"; $subject = "Web Contact Data"; $fields = array(); $fields{"Name"} = "Name"; //not in use, keep for other form $fields{"Email"} = "Email"; $fields{"Phone"} = "Phone"; $fields{"Message"} = "Message"; $body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } if($from == '') { print "You have not entered an email, please go back and try again";} else { $send = mail($to, $subject, $body, $headers); $send2 = mail($from, $subject2, $autoreply, $headers2); if($send) { header( "Location: http://www.websites.cx/indextest.html" ); ##Line22 exit(); ## ALWAYS INCLUDE AN EXIT AFTER A HEADER Redirect. Otherwise, the PHP engine will continue executing code. } else { print "We encountered an error sending your mail, please notify webmaster@YourCompany.com"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/267095-cannot-modify-header-error/#findComment-1369994 Share on other sites More sharing options...
Christian F. Posted August 16, 2012 Share Posted August 16, 2012 Check the error message again: There are two filenames and line numbers reported in it, and I suspect that the first pair is from a different file than what you have the above code in. Quote Link to comment https://forums.phpfreaks.com/topic/267095-cannot-modify-header-error/#findComment-1370004 Share on other sites More sharing options...
wright67uk Posted August 16, 2012 Author Share Posted August 16, 2012 Hi Christian, just checked this is all i get; Warning: Cannot modify header information - headers already sent by (output started at /home/content/w/o/o/myaccounthere/html/websitescx/contact.php:1) in /home/content/w/o/o/woodside09/html/websitescx/contact.php on line 22 Quote Link to comment https://forums.phpfreaks.com/topic/267095-cannot-modify-header-error/#findComment-1370007 Share on other sites More sharing options...
Christian F. Posted August 16, 2012 Share Posted August 16, 2012 And there you have it, where output was started first. I suspect either a whitespace, or a BOM. (Google will help you with the last one.) Quote Link to comment https://forums.phpfreaks.com/topic/267095-cannot-modify-header-error/#findComment-1370010 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.