homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

header error

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

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)

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)

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)

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)

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)

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)

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)

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

function Redirect($url) {
if(headers_sent()) {
echo "<script type='text/javascript'>location.href='$url';</script>";
} else {
header("Location: $url");

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved