0

我知道这个问题已经被问过很多次了,但是,我似乎找不到与我的情况相关的解决方案,因为它们主要处理 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 行,那么它通常是开头的空格、文本 > 或 HTML

4

1 回答 1

5

您的<?php标签、HTML 或其他类型的“输出”之外或上方可能有空格。甚至可能是一个字节顺序标记

<?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 标记。它可以防止在包含的文件之后无意包含空白。”


于 2013-08-08T22:38:20.090 回答