Php Header位置失败*带*错误

时间:2012-01-02 19:48:57

标签: php redirect header

已解决 - 我的编码存在问题:请参阅:Fix incorrectly displayed encoding on an html document with php

我在这里遗漏了一些明显的东西。我的process.php表单处理器没有正确重定向。

我的代码(短版)

header(sprintf("Location: %s", $insertGoTo));
header("Location: index.html");    
echo 'y u no work?', $insertGoTo;

“你没有工作”行回声,页面不刷新(或抛出任何错误)

我从另一个页面中获取此代码,该页面正常工作。它在相同的托管包,所以有问题的PHP版本或任何链接。任何见解将不胜感激。

我的代码的长版本:

<?php ob_start(); ?>
<?php
$submitdate = date("F j, Y");

if (((isset($_POST["email"])) and ($_POST["email"]!="")) and ((isset($_POST["favorite-pet"])) and ($_POST["favorite-pet"]==""))) {

    $name = $_POST["name"];
    $name = str_replace("\r", "", $name);
    $name = str_replace("\n", "", $name);

    $phone = $_POST["phone"];
    $phone = str_replace("\r", "", $phone);
    $phone = str_replace("\n", "", $phone);

    $email = $_POST["email"];
    $email = str_replace("\r", "", $email);
    $email = str_replace("\n", "", $email);

    $comments = $_POST["comments"];
    $comments = str_replace("\r", "", $comments);
    $comments = str_replace("\n", "", $comments);


    $message = $submitdate."\r\n\r\n";
    $message .= "Name: ".$name."\r\n";
    $message .= "Phone: ".$phone."\r\n";
    $message .= "Email: ".$email."\r\n\r\n";
    $message .= "Comments: \r\n";
    $message .= $comments."\r\n\r\n";

    $find = array("/bcc\:/i","/Content\-Type\:/i","/cc\:/i","/to\:/i");

    $email = preg_replace($find, "", $email);
    $message = preg_replace($find, "", $message);

    $recipient = "example@example.com";


    /* subject */
    $subject = "Contact Inquiry";

    $toaddress = $recipient;
    $toname = $recipient;

    $fromaddress = $email;
    $fromname = $name;

    $headers = "From: " . $name ."<" . $email .">\r\n";
    $headers .= "Bcc:  example@example.com\r\n";
    $headers .= "Reply-To: " . $email . "\r\n";
    $headers .= "Return-path: " . $email;


    if(mail($recipient, $subject, stripslashes($message), $headers)) {

        $insertGoTo = "sent.html";
        header(sprintf("Location: %s", $insertGoTo));

    } else {

        echo "Message was not sent <p>";
        exit;

    }

} else {

    $insertGoTo = "index.html";
    header(sprintf("Location: %s", $insertGoTo));
    header("Location: index.html");    
    echo 'y u no work?', $insertGoTo;
}
?>

修改

好的,我改变了一些事情,但仍有问题

我将顶级情侣行改为:

<?php ob_start();
error_reporting(E_ALL);

标题()排到:

$insertGoTo = "index.html";
header(sprintf("Location: %s", $insertGoTo));
exit;

我仍然收到此错误:

Warning: Cannot modify header information - headers already sent by (output started at .../process.php:1) in .../process.php on line 76

4 个答案:

答案 0 :(得分:2)

这是您的错误消息:

  

警告:无法修改标题信息 - 已在第76行的... / process.php中通过(输出于... / process.php:1 开始)发送的标题

我会将其作为答案发布,因为人们显然会忽略它:How to fix "Headers already sent" error in PHP

答案 1 :(得分:0)

重定向后抛出一个退出。

答案 2 :(得分:0)

您的错误建议更改此前三行......

<?php ob_start(); ?>
<?php
$submitdate = date("F j, Y");

对此...

<?php
ob_start();
$submitdate = date("F j, Y");

要么ob_start函数回显某些东西(这将导致已经发送的标题)。

答案 3 :(得分:0)

如果您在文件<?php

中打开process.php标记之前有任何空格或其他“可打印”字符,请检查

错误说明:(output started at .../process.php:1