Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/267095-cannot-modify-header-error/
Share on other sites

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.

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?

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. 

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";
  }
}

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? ;)

@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).

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.

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.

 

mvc_diagram.png

 

Here's some reading material. It is a bit lengthy.

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.

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";
  }
}
?> 

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.