PHP表单:无法修改标头信息 - 已发送的标头

时间:2013-08-08 22:30:55

标签: php redirect contact-form

我知道这个问题已被多次询问,但是,我似乎无法找到与我的情况相关的解决方案,因为它们主要涉及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";} 
 }
 ?>

电子邮件发送得很好,但我得到重定向的名义错误:

警告:无法修改标题信息 - 第23行/home/wills5/public_html/send_email.php中已经发送的标题(在/home/wills5/public_html/send_henry.php:1开始输出)

编辑:显然是第1行之前的空白,谢谢你们。

  

如果消息显示错误在第1行,那么它通常在开放之前引导空格,文本&gt;或HTML

1 个答案:

答案 0 :(得分:5)

您可能在<?php标记,HTML或其他类型的“输出”之外或之上有空格。甚至可能是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标记是好的形式。它可以防止无意中包含白色包含文件后的空格。“