Monday 5 December 2016

PHP - cannot modify header information - headers already sent by (output started at



I am working with PHP and I got this message




cannot modify header information - headers already sent by (output
started at





after executing a code which looks like this:



    if (condition) {

if (condition) {
//Statement
}


$to = $_POST['email'];
$subject = "Registration Confirmation";

$message = '


'.$subject.'


';


// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

// More headers
$headers .= 'From: ' . "\r\n";

mail($to,$subject,$message,$headers);


header('Location: reset.php?username='.$username);
exit();

}else {
//statement
}


Based on this answer from Stackoverflow, I think the error is coming from the HTML code in the variable $message. I don't really know how I can modify the content of that variable to avoid the error.




Kindly help me solve this problem.


Answer



From php documentation:




Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.




So move header('Location: reset.php?username='.$username); before any other kind of output.




By the way, this question was already answered:




No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...