Warning: Cannot modify header information - headers already sent


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:

$mail = new PHPMailer();
>Host = "localhost";
>SMTPAuth = true;
>SMTPDebug =2;
>Username = "system@domain.com";
>Password = "passypass";
>From = "system@domain.com";
>FromName = "The Snder's Name";
$mail->AddAddress("ayam@gmail.com","Attention - System Administrator");
>Subject = "The Subject";
>Body = "$message";
>Send()) {
echo "
> Message could not be sent. >";
echo "Mailer Error: " . $mail-
//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-&gt;Send()) {
echo "&lt;p&gt; Message could not be sent. &lt;/p&gt;";
echo "Mailer Error: " . $mail-&gt;ErrorInfo;

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



Popular posts from this blog

Check SecStore KeyPhrase of Java Stack

Python: thread.error: can't start new thread