php重定向不起作用-我正在使用header()


php redirect not working - I am using header()

我有一个提交表单的php文件。我在index.html中键入信息,然后它将信息发送到email.php。我试图在表单提交后重定向回index.html,但我收到了错误:"警告:无法修改头信息-已经发送了头"

我尝试过在php文件的每一行使用header("Location:http://www.mywebpage.com/index.html");,但都没有成功。下面是我的代码。有什么想法吗?:

<?php
$filename = "email.csv";
$exists = (file_exists($filename));
$handle = fopen($filename, 'a');
$msg = "Thank you for your appointment.'n";//EMail message
$stringToAdd="";    //File into
if (!$exists){
    foreach($_POST as $name => $value) {
        $stringToAdd.="$name,";
    }
    $stringToAdd.="'n";
    $new=False;
    fwrite($handle, $stringToAdd);
}
$stringToAdd="";
foreach($_POST as $name => $value) {
    print "$name : $value<br>";
    $msg .="$name : $value'n";
    $stringToAdd.="$value,";
}
$stringToAdd.="'n";
fwrite($handle, $stringToAdd);
//now close the file
fclose($handle); 
$to = $_POST["uniqname"]."@tobey.com";
$headers = "From: ". "UMSI CDO" ."<umsi.careers@tobey.com>'r'n";
mail($to, 'Appointment Scheduled', $msg, $headers); // 'Appointment Scheduled' is the subject
echo "Email sent";
?>

去掉php关闭标记?>和php打开标记<?php之前的空格、html和空行。同时检查之前是否没有输出:

header("Location:http://www.mywebpage.com/index.html");

printvar_dumpecho等。

您需要删除此print "$name : $value<br>";,因为在调用header() 之前无法输出任何内容