Warning: Cannot modify header information - headers already sent
Hi,
this "Warning: Cannot modify header information - headers already sent General ... The "headers already sent" error i encountered all this while is caused by one thing only.
And this one thing is that data already sent/echoed out PRIOR to header() function is called. Of course this can be two things also:
1) there is a 'whitespace' before your <?php sign. This may be caused by text editor that encodings is dif than UTF-8, this on is easily fixed, justs earch and delete the stupid fsking whitespaces.
2) Well, like i said, there already some data sent/echoed PRIOR to header() function. header() function hsould be the first thing sent out to browsers, before anything else. Well, lets have an example:
this "Warning: Cannot modify header information - headers already sent General ... The "headers already sent" error i encountered all this while is caused by one thing only.
And this one thing is that data already sent/echoed out PRIOR to header() function is called. Of course this can be two things also:
1) there is a 'whitespace' before your <?php sign. This may be caused by text editor that encodings is dif than UTF-8, this on is easily fixed, justs earch and delete the stupid fsking whitespaces.
2) Well, like i said, there already some data sent/echoed PRIOR to header() function. header() function hsould be the first thing sent out to browsers, before anything else. Well, lets have an example:
require("include/class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail-
>
Host = "localhost";
$mail-
>
SMTPAuth = true;
$mail-
>
SMTPDebug =2;
$mail-
>
Username = "system@domain.com";
$mail-
>
Password = "passypass";
$mail-
>
From = "system@domain.com";
$mail-
>
FromName = "The Snder's Name";
$mail-
>
AddAddress("ayam@gmail.com","Attention - System Administrator");
$mail-
>
IsHTML(false);
$mail-
>
Subject = "The Subject";
$mail-
>
Body = "$message";
if(!$mail-
>
Send()) {
echo "
>
Message could not be sent.
>
";
echo "Mailer Error: " . $mail-
>
ErrorInfo;
exit;
}
//redirect the to enquiry_thank_you page
header('Location: order-thank-you.php');
This was a real example. At first, the user fails to send out email using this script, so he added the if(!$mail->Send()) to catch the debug message of SMTP server. Well notice the debug value is set to "2" which will throw out all message response by SMTP server. wheter the script failed or success, the remote SMTP server would still send a response, even a "OK - Messg Queued bla2..". and what else, after the success sent, the script supposed to redirect user to a thank you page, using the header() function. But this script does not work. as the if(!$mail->Send()) already sent out the "Ok.." message to browser, means already there has been data sent PRIOR to the header() function was called. thus the "header already sent thingy..
How do we overcome? Well, two way.
1) just comment out the whole if(!$mail->Send()) thing.
if(!$mail->Send()) {
echo "<p> Message could not be sent. </p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
and voila! the things a charm.
2) Use javascript instead. such as:
//redirect the to enquiry_thank_you page
// header('Location: order-thank-you.php');
print "<script language='javascript'>location.href=\"order-thank-you.php
So basically. either way will help you redirects user. Ouh, one more thing, if you know the script is OKie, but the exception is still there, and you're on Linux environment, you can simply ad a .htaccess file inn same folder with below syntax:
php_value error_reporting E_NONE
Erm.. my explanation only based on my logic and understanding and does not reprsents the actual working of PHP. So this might be wrong. Refer at your won risk XD~
Kudos C.L.Lee
//alak
Comments
Post a Comment