“无法修改标题信息-标题已发送...”PHP邮件表单错误代码[重复]

pcww981p  于 2023-02-15  发布在  PHP
关注(0)|答案(2)|浏览(189)
    • 此问题在此处已有答案**:

How to fix "Headers already sent" error in PHP(11个答案)
昨天关门了。
当前正在尝试为个人网站创建电子邮件联系人表单。header()重定向功能不起作用,并不断返回错误消息:
"PHP警告:无法修改标头信息-标头已由(输出开始于/home1/claireel/public_html/email. php:1)发送到/home1/claireel/public_html/email. php中的第38行"
此外,表单将发送电子邮件到测试电子邮件,我原来在文件中,但当我改变了地址,我实际上打算使用,它不会发送。

<?php
$errors = [];
$errorMessage = '';

if (!empty($_POST)) {
   $first = $_POST['first'];
   $last = $_POST['last'];
   $email = $_POST['email'];
   $message = $_POST['message'];

   if (empty($first)) {
       $errors[] = 'First name is empty';
   }

   if (empty($last)) {
    $errors[] = 'Last name is empty';
}

   if (empty($email)) {
       $errors[] = 'Email is empty';
   } else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
       $errors[] = 'Email is invalid';
   }

   if (empty($message)) {
       $errors[] = 'Message is empty';
   }

   if (empty($errors)) {
       $toEmail = '************@gmail.com';
       $emailSubject = 'New email from your contact form';
       $headers = ['From' => $email, 'Reply-To' => $email, 'Content-type' => 'text/html; charset=utf-8'];
       $bodyParagraphs = ["Name: {$first} {$last}", "Email: {$email}", "Message:", $message];
       $body = join(PHP_EOL, $bodyParagraphs);

       if (mail($toEmail, $emailSubject, $body, $headers)) {
           header('Location: thank-you.html');
       } else {
           $errorMessage = 'Oops, something went wrong. Please try again later';
       
       }

   } else {

       $allErrors = join('<br/>', $errors);
       $errorMessage = "<p style='color: red;'>{$allErrors}</p>";
   }
}

?>

我已经确认了在开始和结束的php标签之前没有空格,我也确认了文件是用UTF-8编码的,没有BOM,我也没有看到文件中有任何东西会在头函数之前输出。

dffbzjpn

dffbzjpn1#

将出口置于

if (mail($toEmail, $emailSubject, $body, $headers)) {
    header('Location: thank-you.html');
} else {
    $errorMessage = 'Oops, something went wrong. Please try again later';
}

在标头()之后:

if (mail($toEmail, $emailSubject, $body, $headers)) {
    header('Location: thank-you.html');
    exit;
} else {
    $errorMessage = 'Oops, something went wrong. Please try again later';
}
wyyhbhjk

wyyhbhjk2#

以下是可以解决问题的步骤。如果在将任何输出发送到浏览器之前未使用ob_start()启用输出缓冲,则会出现此问题。header()函数调用可能会失败,并显示类似于前面提到的错误消息("无法修改标题信息-标题已发送...")。这是因为在将任何输出(包括任何空白或HTML标记)发送到浏览器之前必须调用header()函数。
ob_start()的输出缓冲允许您在将任何输出发送到浏览器之前捕获和操作它,这对于需要确保在发送任何输出之前调用header()函数的情况非常有用。
如何修改代码,通过ob_start()使用输出缓冲来修复头错误:
ob_start()函数放置在PHP文件的最开头,在任何输出发送到浏览器之前。这将启用整个脚本的输出缓冲,在任何输出发送到浏览器之前捕获它。
然后,将header('Location: thank-you.html')行替换为ob_end_clean();header('Location: thank-you.html');。这将丢弃输出缓冲区捕获的任何输出,并使用干净的输出缓冲区发送header()函数调用。
下面是修改后的代码:

<?php
ob_start();

$errors = [];
$errorMessage = '';

// Rest of your code

if (empty($errors)) {
    $toEmail = '************@gmail.com';
    $emailSubject = 'New email from your contact form';
    $headers = ['From' => $email, 'Reply-To' => $email, 'Content-type' => 'text/html; charset=utf-8'];
    $bodyParagraphs = ["Name: {$first} {$last}", "Email: {$email}", "Message:", $message];
    $body = join(PHP_EOL, $bodyParagraphs);

    if (mail($toEmail, $emailSubject, $body, $headers)) {
        ob_end_clean();
        header('Location: thank-you.html');
        exit; // Always exit after a header redirect
    } else {
        $errorMessage = 'Oops, something went wrong. Please try again later';
    }
}

// Rest of your code

ob_end_flush();
?>

通过此修改,在header()函数调用之前生成的任何输出都将被输出缓冲区捕获,并在调用header()函数之前与ob_end_clean()一起丢弃。这应该修复了"无法修改标头信息"错误。
如果问题仍然存在,您可以使用JavaScript通过简单的回显来重定向页面:

<?php
echo '<script>window.location.href = "page.php";</script>';
?>

相关问题