PHP形式:无法修改标头信息-标头已发送
我知道这个问题已经问过很多次了,但是,我似乎找不到适合我情况的解决方案,因为它们主要处理wordpress。
I know that this question has been asked many times, however, I can't seem to find solutions that are relevant to my situation, since they mostly deal with wordpress.
这是我的邮件表单:
<?php
$to = "email@gmail.com" ;
$from = $_REQUEST['email'] ;
$name = $_REQUEST['name'] ;
$headers = "From: $from";
$subject = "Contact Submission From domain.com";
$fields = array();
$fields{"name"} = "name";
$fields{"title"} = "title";
$fields{"email"} = "email";
$fields{"phone"} = "phone";
$fields{"prefer_phone"} = "pref_phone";
$fields{"prefer_email"} = "pref_email";
$fields{"message"} = "message";
$fields{"referral"} = "referral";
$body = "Here is their submitted message:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n\n",$b,$_REQUEST[$a]); }
if($from == '') {print "You have not entered an email, please hit back and resubmit";}
else {
$send = mail($to, $subject, $body, $headers);
if($send)
{header( "Location: http://www.domain.com/sent.html" );}
else
{print "We encountered an error sending your mail, please notify support@domain.com";}
}
?>
电子邮件发送正常,但重定向时出现名义错误:
The email sends just fine, but I get the titular error for the redirect:
警告:无法修改标头信息-已在/home/wills5/public_html/send_email.php上发送(由/home/wills5/public_html/send_henry.php:1开始的输出)第23行
Warning: Cannot modify header information - headers already sent by (output started at /home/wills5/public_html/send_henry.php:1) in /home/wills5/public_html/send_email.php on line 23
编辑:显然,这是第1行之前的空白,谢谢大家。
It was frickin' whitespace before line 1 apparently, thanks guys.
如果消息表明错误在第1行,则通常是在开头
If the message says the error is in line 1, then it is typically leading whitespace, text >or HTML before the opening
前的空白,文本>或HTML前导
您有可能在<?php
标记或HTML或其他某种类型的输出的上方或上方有空格。甚至字节顺序标记。
Chances are you have whitespace besides or above your <?php
tag, or HTML or some other type of "output". Maybe even a byte order mark.
<?php
echo "Correct";
// or vvv, but not with echo
// header("Location: http://www.example.com/");
?>
(space)
(space) <?php
echo "Incorrect";
header("Location: http://www.example.com/");
?>
<div align="center">Text</div>
<?php
echo "Incorrect";
header("Location: http://www.example.com/");
?>
脚注:根据Gavin的建议,我引用:出于这个原因,在类文件上保留关闭的php标记是一种很好的形式。它可以防止在包含文件之后无意间包含空格。
Footnote: As per Gavin's suggestion, and I quote: "It is good form to leave off the closing php tag on class files for this reason. It prevents the inadvertent inclusion of white-space after an included file."