/    Sign up×
Community /Pin to ProfileBookmark

What’s the cause of geting "Cannot add header information – headers already sent by""

What’s the cause of geting “Cannot add header information – headers already sent by…….. on line …….”

The line is this ” mail($incoming_mailto, $incoming_subject, $message, $headers); “

and it tells me that header already sent by the line ” header(“Location: $incoming_thanks”); “

What’s wrong?

Ps:i’m trying to setup a page on my PC and when i want it to send email it gives me this error

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@Natcon67Jun 13.2005 — I am not sure that is the line that is incorrect. Can you post the entire code?
Copy linkTweet thisAlerts:
@bokehJun 14.2005 — Make sure there is no white space or anything else before the first <?php tag.

Then if it is still happening just add this right at the top of the script:[code=php]<?php
ob_start();[/code]
Copy linkTweet thisAlerts:
@condorauthorJun 14.2005 — I'm useing Dynaform,i dont know if it works on a PC page.If u know 1 that work pls tell me ?.

<?

$from_address = "";

$required_on = "no";

$required_errorpage = "error.html";

$override = "no";

$incoming_mailto = "[email protected]";

$incoming_subject = "You have a new message from your DynaForm.";

$incoming_thanks = "thanks.html";

$ban_ip_on = "no";

$ban_ip_list = "111.222.33.55,11.33.777.99";

$secure_domain_on = "yes";

$autorespond_on = "no";

$autorespond_subject = "Your Form Submission";

$autorespond_from = "[email protected]";

$autorespond_contents = "Your submission from our website has been received. Thank you!";

$autorespond_mailto_field = "Email";

if($HTTP_SERVER_VARS['REQUEST_METHOD'] == "GET") {

echo "

<html>

<head><title>Webligo PHP DynaForm is installed correctly.</title></head>

<body>

<font style='font-family: verdana, arial; font-size: 9pt;'>

<b>DynaForm is installed correctly.</b></font><br>

<font style='font-family: verdana, arial; font-size: 8pt;'>

DynaForm Easy PHP Form Mailer was created by <a href='http://www.webligo.com'>Webligo Developments</a>.

</font>

</body></html>

";

exit();

}

$incoming_fields = array_keys($HTTP_POST_VARS);

$incoming_values = array_values($HTTP_POST_VARS);

if($override == "no") {

$incoming_mailto = @$HTTP_POST_VARS['rec_mailto'];

$incoming_subject = @$HTTP_POST_VARS['rec_subject'];

$incoming_thanks = @$HTTP_POST_VARS['rec_thanks'];

}

$incoming_mailto_cc = @$HTTP_POST_VARS['opt_mailto_cc'];

$incoming_mailto_bcc = @$HTTP_POST_VARS['opt_mailto_bcc'];

$form_url = @$HTTP_REFERER;

if($secure_domain_on == "yes") {

$form_url_array = parse_url($form_url);

$form_domain = $form_url_array[host];

if($form_domain != $HTTP_SERVER_VARS[HTTP_HOST]) {

echo "<h2>DynaForm Error - Invalid Domain</h2>

You have accessed DynaForm from an external domain - this is not allowed.<br>

You may only submit forms to a DynaForm file that exists on the same domain name.<br>

If you believe to be receiving this message in error, please refer to your readme.txt file.

<br><br>";

$error = "yes";

}

}

if($incoming_mailto == "") {

echo "<h2>DynaForm Error - Missing Field</h2>

Your form located at <a href='$form_url'>$form_url</a> does not work because you forgot to include

the required "<b>rec_mailto</b>" field within the form. This field specifies who the email will

be sent to.

<br><br>

This should look like:<br>

<input type="hidden" name="rec_mailto" value="[email protected]">

<br><br>

If you are still confused, please refer to the readme.txt for more information and examples.<br><br><br><br>

";

$error = "yes";

}

if($incoming_subject == "") {

echo "<h2>DynaForm Error - Missing Field</h2>

Your form located at <a href='$form_url'>$form_url</a> does not work because you forgot to include

the required "<b>rec_subject</b>" field within the form. This field specifies the subject of

the email that will be sent.

<br><br>

This should look like:<br>

<input type="hidden" name="rec_subject" value="New DynaForm Email">

<br><br>

If you are still confused, please refer to the readme.txt for more information and examples.<br><br><br><br>

";

$error = "yes";

}

if($incoming_thanks == "") {

echo "<h2>DynaForm Error - Missing Field</h2>

Your form located at <a href='$form_url'>$form_url</a> does not work because you forgot to include

the required "<b>rec_thanks</b>" field within the form. This field specifies what page the user

will be taken to after they submit the form.

<br><br>

This should look like:<br>

<input type="hidden" name="rec_thanks" value="thanks.html">

<br><br>

If you are still confused, please refer to the readme.txt for more information and examples.<br><br><br><br>

";

$error = "yes";

}

if($ban_ip_on == "yes") {

if(strstr($ban_ip_list, $HTTP_SERVER_VARS[REMOTE_ADDR])) {

echo "<h2>DynaForm Error - Banned IP</h2>

You cannot use this form because your IP address has been banned by the administrator.<br>

";

$error = "yes";

}

}


if($error == "yes") {

exit();

}

$message = "This email was received from your DynaForm located at $form_url nn";

for ($i = 0; $i < count($incoming_fields); $i++) {

if($incoming_fields[$i] != "rec_mailto") {

if($incoming_fields[$i] != "rec_subject") {

if($incoming_fields[$i] != "rec_thanks") {

if($incoming_fields[$i] != "opt_mailto_cc") {

if($incoming_fields[$i] != "opt_mailto_bcc") {

if($required_on == "yes") {

$sub = substr($incoming_fields[$i], 0, 2);

if($sub == "r_") {

if($incoming_values[$i] == "" OR !isset($incoming_values[$i]) OR $incoming_values[$i] == " ") {

header("Location: $required_errorpage");

exit();

}}}

$message .= "$incoming_fields[$i]:n$incoming_values[$i]nn";

}}}}}}

$message .= "nnThank you for using our Webligo DynaForm script.nWe ask that you please link back to our site if you have not already.nYour use of DynaForm is subject to the license agreement outlined in dynaform.php.nVisit us at: http://www.webligo.com";

$headers = "";

if($from_address != "") {

$headers .= "From: $from_addressrn";

}

if($incoming_mailto_cc != "") {

$headers .= "Cc: $incoming_mailto_ccrn";

}

if($incoming_mailto_bcc != "") {

$headers .= "Bcc: $incoming_mailto_bccrn";

}

mail($incoming_mailto, $incoming_subject, $message, $headers);

if($autorespond_on == "yes") {

$autorespond_mailto = @$HTTP_POST_VARS[$autorespond_mailto_field];

$autorespond_headers = "From: $autorespond_from";

mail($autorespond_mailto, $autorespond_subject, $autorespond_contents, $autorespond_headers);

}

header("Location: $incoming_thanks");


?>



Ps:i'm kinda n00b at this :p
Copy linkTweet thisAlerts:
@amazing_andr3Jun 14.2005 — Then if it is still happening just add this right at the top of the script:[code=php]<?php
ob_start();[/code]
[/QUOTE]


Better add this to all your scripts:
[code=php]ob_start('ob_gzhandler');[/code]

Now you don't have to worry about sending output before headers, and as a bonus you're compressing the output and that will give the user some 25% decrease in loading time
Copy linkTweet thisAlerts:
@bokehJun 14.2005 — that will give the user some 25% decrease in loading time[/QUOTE]

That is not quite true. People don't like waiting at a blank screen. Normal content is loaded into the browser as it arrives packet by packet. If a page is GZIPed the client has to wait for it to be completely received and unzipped (unzip info comes at the end of the file) before anything is displayed. Also if there is CSS in an external file this can not be requested until the GZIPed html file has been unzipped. If the CSS is also GZIPed nothing will be displayed until that is unzipped too. So, to sum up, if content is GZIPed nothing is displayed until the last consecutive item has been unzipped. With non zipped content the external CSS can be requested as soon as the head of the html page has been recieved. With GZIP the requests consecutive where as with uncompressed content they are concurrent.

Because the requests are not concurrent with GZIP even though there is less data sent that data usually takes longer to arrive and certainly much longer to be displayed. Can you imagine having to wait for every html page to be completely downloaded before you could start viewing it. Just think how often you close a page before it has finish loading because you can see from the first few lines that it is not what you wanted.

GZIP does save a small amount of compression on the wire but text is small anyway and images are already compressed.

One interesting feature of GZIP though is the way the data appears on the screen. With uncompressed content it loads progressively but with GZIP because you wait for all content to be received before loading when it does load it just goes from a blank screen to fully formated and displayed content in an instant.
Copy linkTweet thisAlerts:
@amazing_andr3Jun 14.2005 — With a table layout, all the HTML needs to be loaded. Otherwise, the page will jump around like hell. The rows load in one place, then jump as more of the table is loaded. I find that highly annoying. So the behaviour you described is desired in this case.

I have found an increase in speed after using gz_handler. The file size is decreased by 60-75%, if I remember correctly. However, because the reasons you presented, the increase in speed is only 25%. In my opinion, that justifies the 'blank screen'.

But you know, most of the times I open new tabs in the background, and I never select them until I see they have fully loaded. So I almost never look at the blank, or partially loaded screen.
Copy linkTweet thisAlerts:
@bokehJun 14.2005 — Using tables for layout is just plain wrong, but a discussion I don't want to get into. I recently did an experiment with some friends.

  • 1. HTML file, 5k, with external CSS, both uncompressed.

  • 2. Same HTML file GZIPed and with the CSS between style tags in the head section.

  • 3. Same HTML file, with external CSS, both GZIPed.


  • I had several people try these but didn't tell them what the test was for (blind study). I just asked them which they thought was best and why and they all replied with the above order and that case 3 was extremely slow (an extra couple of seconds).
    Copy linkTweet thisAlerts:
    @amazing_andr3Jun 14.2005 — I don't use tables, but I see a lot of other people still do, so it's a case that needs to be discussed.

    I don't get it. Why is 3 slower than 2? Doesn't all the CSS need to be loaded for the page to be displayed? In this case 3 should be faster than 2.

    If this were not the case, you would run in the same problem with tables. The page would change as the browser reads more of the CSS.

    <edit>oh wait, i see the CSS was in the HTML... that's why 2 is faster, less requests. nevermind. But who puts the CSS in the HTML anyway?

    OK another question, then. CSS qziped and HTML not gziped. What about this?
    Copy linkTweet thisAlerts:
    @bokehJun 14.2005 — The reason is twofold,

    1) in case 2 less data is sent because even if the CSS page is cached the browser still sends the request headers to the server which come to between 1-2k. The GZIPed CSS is not that big (unless it is bloated). 3k of CSS compresses down to about 1.3k. but...

    2) the request for external CSS is not even sent until the first document is unzipped.

    So in case 2 once we unzip the HTML file we are ready to display it but in case 3 once we finish unzipping we find out we need to request another file which needs to be download and unzipped before we can display anything.
    ×

    Success!

    Help @condor spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.23,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...