Contact Form Error

KeybladeSephi

New Member
Messages
19
Reaction score
0
Points
0
Alright guys, I bet you've seen a lot of these and I'm sorry if it's getting a little redundant (it's not my fault!). Basically, my contact form is fine, but after I tested it, it had an error: "Warning: Cannot modify header information - headers already sent by (output started at /home/keyblade/public_html/contact.php:3) in /home/keyblade/public_html/contact.php on line 27". However, the email is still successfully sent to me. Here's my contact form and here is a screenshot of my problem. Here is the code from my "contact.php" document:

HTML:
<link rel="stylesheet" href="../css/main.css" />

<?php 

$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['comments_or_questions'];
$valid_email = false;

if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) 
{
  $valid_email=true;
}

if(!$name or !$comments or ($valid_email==false))
{
    echo ("You didn't fill in all the required fields.<br />Please <a href=\"javascript:history.back()\">return</a> to the previous page and try again.");
}
else
{
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= 'From: '.$name.' <'.$email.'>' . "\r\n";
    $headers .= 'Reply-To: '.$email. "\r\n";    
    
    mail("KeybladeSephi@netscape.net","Email From Site", stripslashes($comments), $headers);
    header("Location: http://keybladesephi.exofire.net"); /* Redirect browser */
}
?>

Line 27 is " header("Location: http://keybladesephi.exofire.net"); /* Redirect browser */ "

If anyone can help me, that'd be greatly appreciated; +1 rep =]
 

leafypiggy

Manager of Pens and Office Supplies
Staff member
Messages
3,819
Reaction score
163
Points
63
are you including any other PHP files? cause if you are...they could be causing the problems.
 

Livewire

Abuse Compliance Officer
Staff member
Messages
18,169
Reaction score
216
Points
63
Problem is right here: (first line)

Code:
<[COLOR=#000080]link rel=[COLOR=#0000ff]"stylesheet"[/COLOR] href=[COLOR=#0000ff]"../css/main.css"[/COLOR] />[/COLOR]

In order to send a header via php, NOTHING can already be sent to the browser. Since you sent that link tag, the header is no longer able to be modified.

Should be easy enough to fix, rip that out and it should work just fine.


Edit: If you need that for the error message, just have PHP echo it back before echoing back the error. Just gotta make sure that if it's a valid email and you want to change the header that nothing's been sent to the browser yet.
 
Last edited:

leafypiggy

Manager of Pens and Office Supplies
Staff member
Messages
3,819
Reaction score
163
Points
63
I didn't even see that....You make me feel stupid... :D
 

Livewire

Abuse Compliance Officer
Staff member
Messages
18,169
Reaction score
216
Points
63
I didn't even see that....You make me feel stupid... :D

It's easy to see when I just made the mistake twice in the last day and a half :)

And posted about twice on another site >_<
 

phpasks

New Member
Messages
145
Reaction score
0
Points
0
<link rel="stylesheet" href="../css/main.css" />

<?php

$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['comments_or_questions'];
$valid_email = false;

if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))
{
$valid_email=true;
}

if(!$name or !$comments or ($valid_email==false))
{
echo ("You didn't fill in all the required fields.<br />Please <a href=\"javascript:history.back()\">return</a> to the previous page and try again.");
}
else
{
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$name.' <'.$email.'>' . "\r\n";
$headers .= 'Reply-To: '.$email. "\r\n";

mail("KeybladeSephi@netscape.net","Email From Site", stripslashes($comments), $headers);
header("Location: http://keybladesephi.exofire.net"); /* Redirect browser */
}
?>
PHP:
header("Location: http://keybladesephi.exofire.net"); /* Redirect browser */
exit;

now try it


Asif
http://www.phpasks.com
 

Livewire

Abuse Compliance Officer
Staff member
Messages
18,169
Reaction score
216
Points
63
PHP:
header("Location: http://keybladesephi.exofire.net"); /* Redirect browser */
exit;
now try it


Asif
http://www.phpasks.com


Will have the same exact problem - the <link> tag has already been sent to the browser, and as such we can no longer alter the headers. Sending the <link> tag had PHP send the headers so the browser knew it was rendering a webpage, can't modify it anymore.

Dunno if the exit helps or not but it still won't fix the original problem (he needs to either erase or move that line)
 

KeybladeSephi

New Member
Messages
19
Reaction score
0
Points
0
Problem is right here: (first line)

Code:
<[COLOR=#000080]link rel=[COLOR=#0000ff]"stylesheet"[/COLOR] href=[COLOR=#0000ff]"../css/main.css"[/COLOR] />[/COLOR]
In order to send a header via php, NOTHING can already be sent to the browser. Since you sent that link tag, the header is no longer able to be modified.

Should be easy enough to fix, rip that out and it should work just fine.


Edit: If you need that for the error message, just have PHP echo it back before echoing back the error. Just gotta make sure that if it's a valid email and you want to change the header that nothing's been sent to the browser yet.

Yep, that indead was the problem. +1 rep!! =]. Now is there anyway to still implement a style onto my "contact.php" document without interfering with the rederecting header? I've tried to put a style in there to change the background at the very top of my code. Should I put the style underneath the "<?php" tag? If there is any other way, please tell me =]. Again, another +1 rep.
 
Top