Welcome to WebmasterWorld Guest from 54.162.141.212

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

header error

     
7:58 pm on Oct 17, 2007 (gmt 0)

5+ Year Member



I know this question has been answered a million times, but I am thick and can't get it down.

I continue to get the "header already sent..." message and I have checked for whitespace, and html before php and all of the solutions that are suggestion on these boards. Still I am not getting it right. The email form works, but the redirect does not.

I am really new to this (in case you couldn't figure that out yourself) so thank you for any help you can offer.

<?
$name = $_POST['name'];
$email = $_POST['email'] ;
$phone = $_POST['phone'];
$message = $_POST['comments'] ;
mail( "kathy@example.com", "Information Request from Website",
"$message\nPhone Number $phone\n", "From: $name <$email>" );
header( 'Location: http://www.example.com/thankyou.html');
?>

[edited by: dreamcatcher at 8:11 pm (utc) on Oct. 17, 2007]
[edit reason] Use example.com, thanks. [/edit]

8:02 pm on Oct 17, 2007 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Is the code below the only code on the page, or is it an included file?

If this code is not the only thing on the page is it at the very top of the page, or half way down?

Welcome to webmaster world and the wonderful world of example.com (dont worry you are not the first to keep your url as it actually is in the code).

8:03 pm on Oct 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Comment out the line that sends the mail, make sure it's not causing headers to be written, see if it redirects then.
8:18 pm on Oct 17, 2007 (gmt 0)

5+ Year Member



commenting out had the same results.

the php is included in the code for the entire contact page. in the middle. should I move the entire code for the form to the top? I wasnt sure if this would completely screw things up.

and i'm sorry, but i don't know what you mean about example.com
speak slowly, i'm new. :)

8:24 pm on Oct 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Let's see the file it's included in, likely it's the culprit, as if anything is sent to the browser, including plain html, headers are already sent.
9:11 pm on Oct 17, 2007 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Include that code before the opening doc type statement or <html>. The php should be the first thing on the page.

Once you have the first < of the DTD something has been sent to the browser, so you cant send headers, unless you want to get into buffering.

[edited by: PHP_Chimp at 9:13 pm (utc) on Oct. 17, 2007]

9:21 pm on Oct 17, 2007 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Moving the code wont break anything as you are using $_POST variables, so the page actually has to be posted before anything is done. When the page is called the first time there are no post variables, so your script as it stands is doing nothing. Well actually it is trying to send the mail when it has no content.

So you want to check if there is any content. You could just use -

if {$_POST) {
// your code in here
}

This way php wont try to send your mail when there is no content, as it will look to see if anything has been posted. If something has been posted then it will send you mail.

I assume that you are checking the $email filed to check that people are not putting me@spam.com, another@spam.com, another1@spam.com, etc. As otherwise your form will be spamming everyone as soon as the bots find it.

10:38 pm on Oct 17, 2007 (gmt 0)

10+ Year Member




Here is a function that will redirect if the headers are already sent...

###BEGIN REDIRECT IF HEADER ALREADY SENT
function Redirect($url) {
if(headers_sent()) {
echo "<script type='text/javascript'>location.href='$url';</script>";
} else {
header("Location: $url");
}
}
//usage
//Redirect('somewhere.html');
###END REDIRECT IF HEADER SENT

 

Featured Threads

Hot Threads This Week

Hot Threads This Month