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. :( ???
Never mind. It turns out that the header function hates the echo command for some reason. :shrug:
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.
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.