TwelvestoneBack End

PHP Warning: Cannot modify header information


Sign in

  • Waiting for Godot ( 730 k posts )
    Just conversation.
  • Thunder Dome ( 23 k posts )
    Photoshop Tennis and Collabs.
  • Photography ( 5.1 k posts )
    For all you shutterbugs, sh...
  • Flash ( 18 k posts )
    ActionScripting to tweens, ...
  • Front End ( 5.9 k posts )
    general front end design an...
  • Back End ( 9.7 k posts )
    serverside scripting, progr...
  • Projects and Theory ( 12 k posts )
    This forum is for discussio...
  • FAQ ( 269 posts )
    All those nagging questions...
  • Design ( 17 k posts )
    graphics & all aspects of g...
  • Purgatory ( 3.6 k posts )
    12stone Jail, feel free to ...
Candy Beard
 
2010-08-18

I'm trying to modify a simple email script to make it a tiny bit harder for spambots to do their thing. The current script is werkin fine: <?php $name = $_POST['name']; $email = $_POST['email']; $subject = $_POST['subject']; $location = $_POST['location']; $text = $_POST['text']; $formcontent="Name: $name\nEmail: $email\nSubject: $subject\nLocation: $location\nComment:$text"; $recipient = "mark@markclarkson.com"; $subject = "Aerial Quote"; $mailheader = "From: $email\r\n"; $mailheader .= "Reply-To: $email\r\n"; $mailheader .= "MIME-Version: 1.0\r\n"; mail($recipient, $subject, $formcontent, $mailheader) or die("Error: could not send email!"); header('Location: acknowledgement.html'); ?>

However, I'm trying to add a hidden field ('leatherette'), then check to see if it is filled out. If it is filled out, no sendie the email.

... if ($leatherette == "") { echo "You can haz win!" ; mail($recipient, $subject, $formcontent, $mailheader) or die("Error: could not send email!"); header('Location: acknowledgement.html'); } else { echo "You are have fail!" ; } ?>

I put an IF around the send code. But every time I run it, I get a PHP error: > Warning: Cannot modify header information - headers already sent by (output started at /home/content/87/6487087/html/emailtest.php:8) in /home/content/87/6487087/html/emailtest.php on line 20 (line 20 shown in red.)

In fact, no matter how I arrange things - even if I pull header() out of the IF completely and make it the unconditional last statement, I get the same error. :( ???

Candy Beard
 
2010-08-18

Never mind. It turns out that the header function hates the echo command for some reason. :shrug:

Stickman
 
2010-08-18

If you send any output, e.g. by echo()ing a string, then PHP will automatically send standard HTML headers for you because the headers have to come before the content. So sending further headers won't work -- hence the error.

What you're doing makes no sense anyway, because you're sending the output and then immediately redirecting so chances are the user won't even see the message. It would make more sense either to a) not redirect or b) display the message on the redirect target page.

Candy Beard
 
2010-08-18

The echo was merely for my own information whilst debugging. It's not intended for the user and, in fact, is now gone.

Thanks for the info on header(). I figured it was something like that.

Sorry, you must be a member to post to a conversation. Either log in or sign up to get involved.
TwelvestoneBack End

PHP Warning: Cannot modify header information