PDA

View Full Version : header() problem


Steven
December 19th, 2007, 13:54
OK, here is the error msg I get:

Warning: Cannot modify header information - headers already sent by(...)

This is for an email form that I have on my contact page. Here is part of the code:


(... some PHP & HTML Codes...)

<?php

}

else{

$to_mail = "wajih14@gmail.com";

foreach($_POST as $key => $value){

$_POST[$key] = preg_replace("/BCC:/", "**SPAM ATTEMPT**", $value);

mail( $to_mail, $subject, $message, "From: $name <$email>" );
header( "Location: http://www.lightblu.com/4/success.php" );
}
}

else{
?>

(... some more PHP & HTML Codes...)

I think I know where the problem lies... the header shouldn't be after any HTML codes (I think). Where could I place it then? This is the only logical place that it should be.

Help?

inimino
December 20th, 2007, 01:47
In an HTTP message, headers are distinguished from the body by coming before it (separated by a blank line), so sending an additional header once you have already sent the headers and part of the body would be meaningless.

Instead you should do whatever tests you need to do to determine what headers you are going to send, before you send any of the body. Even if what you are trying to do was possible, it would be odd to send half the page and then redirect anyway.

Steven
December 20th, 2007, 14:27
Well the header in thi sis case is used for only 1 purpose: Once the user completed the form (contact form) and presses "Send," the page will re-direct him to a "Success" page that informs the user that his "email" has successfully been sent.

And that's what the header is used for.

Now, since I have the header() function in the middle of my code (it comes after the HTML and PHP code), it just returns that warning. I do get the email (the form that the user fills out sends just fine), but the user gets a warning.

So the question is, where do I put the header() function?

inimino
December 20th, 2007, 15:08
You'll need to move it up towards the top, before your script produces any other output.