【问题标题】:getting warning Header may not contain more than a single header on wordpress收到警告标题在 wordpress 上可能不包含多个标题
【发布时间】:2015-04-14 02:42:32
【问题描述】:

我正在学习 WordPress 和 PHP,并在 www.sunriselodging.com 建立一个网站

尝试在表单中输入数据时,我在第 6 行收到一条 PHP 错误警告,提示“标题可能不包含多个标题”。我尝试了一些解决方案,例如检查与 PHP 无关的 URL。我在这里阅读了一些依赖于 PHP 的解决方案,因此我无法弄清楚这里需要做什么。

我的代码如下:

    <?php

    $reservation_message = $_POST['reservation_message'];

    // Redirect back
Header('Location: '. $_POST['reservation-url'].'?message='.$reservation_message);
echo "<meta charset='utf-8'>";  

// Loading variables from form
$blog_name = $_POST['blog_name'];
$blog_email = $_POST['blog_email'];
$reservation_email_subject = $_POST['reservation_email_subject'];
$reservation_email = $_POST['reservation_email'];
$reservation_email_switch = $_POST['reservation_email_switch'];

$room = $_POST['reservation-room'];
$checkin = $_POST['reservation-checkin'];
$checkout = $_POST['reservation-checkout'];
$people = $_POST['reservation-people'];

$name = $_POST['reservation-name'];
$email = $_POST['reservation-email'];
$phone = $_POST['reservation-phone'];
$message = $_POST['reservation-message'];

if($reservation_email_switch == 'on'){
    // EMAIL TO CLIENT
    // SET info to email
        // From in format NAME <email>
        $from = $blog_name.'<'.$blog_email.'>';

        // Reply to in format NAME <email>
        $reply = $blog_name.'<'.$blog_email.'>';

        // Subject
        $subject = $reservation_email_subject;

        // Message
        $message = $reservation_email;
    //

    $to = $email;
    $headers = 'From: '. $from . "\r\n" .
        'Reply-To: '. $reply . "\r\n" .
        'X-Mailer: PHP/' . phpversion();
    // Send mail
    mail($to,$subject,$message,$headers);
    // END OF EMAIL TO CLIENT
    // ---------------------
}

// EMAIL TO RESERVATION CREW
$message = $_POST['reservation-message'];
$headers = 'From: '. $name . '<' . $email . '>' . "\r\n" .
    'Reply-To: '. $name . '<' . $email . '>' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();
$subject = 'Reservation for room #' . $room;
$message = 'Name: ' . $name . '
Room: #' . $room . '

Check in: ' . $checkin . '
Check out: ' . $checkout . '
Number of people: ' . $people . '

Email: ' . $email . '
Phone: ' . $phone . '

' . $message;

$to = $blog_email;

// Send mail
mail($to,$subject,$message,$headers);
// END OF EMAIL TO RESERVATION CREW
// ---------------------

exit();

?>

表单的 URL 在这里 - http://www.sunriselodging.com/reservation/。显然该表格也没有发送电子邮件。有人可以看看这个并告诉这里的代码有什么问题吗?

如果有人能指导我为初学者提供一个很好的在线 PHP 课程(如果免费的话,那就太好了),我也将不胜感激。

谢谢,

阿迪

【问题讨论】:

  • 标头位置后的任何代码都不会运行
  • 标头运行后的@aadi 代码。您应该始终在标头之后放置和 exit() 或 die() 以防止进一步的代码执行。
  • 感谢您的回复。我对 PHP 完全陌生,无法找出解决方案。你能告诉我我可以用代码做什么来让它工作吗?
  • @aadi php.net/manual/en/function.header.php 你应该做广告'exit;'在你的标题之后。不确定这是否重要,但我认为“标题”应该更低。
  • 感谢@bassxzero,我尝试同时添加 exit();并退出;现在它给出了“(T_CONSTANT_ENCAPSED_STRING)”错误。我该怎么办?

标签: php wordpress error-handling


【解决方案1】:

这个片段的全部问题是你有 1) 没有条件 2) 没有 mail() 函数。

如果您要使用标题,您可能应该有一个条件。您可能还应该有执行电子邮件的条件。最后,您需要在页面上的所有内容之前运行此代码(如果在页面上,可能在 get_header() 之前)。您的工作流程应该类似于以下内容:

<?php
   // I assume you want the form to first process the email then forward
   // so you need to check that the form was submitted
   // Then check that the customer email is valid
   if(isset($_POST['reservation-email']) && filter_var($_POST['reservation-email'], FILTER_VALIDATE_EMAIL)) {
        // Loading variables from form
        $blog_name  = $_POST['blog_name'];
        $blog_email = $_POST['blog_email'];
        $reservation_email_subject = $_POST['reservation_email_subject'];
        $reservation_email = $_POST['reservation_email'];
        $reservation_email_switch = $_POST['reservation_email_switch'];

        $room       =   $_POST['reservation-room'];
        $checkin    =   $_POST['reservation-checkin'];
        $checkout   =   $_POST['reservation-checkout'];
        $people     =   $_POST['reservation-people'];

        $name       =   $_POST['reservation-name'];
        $email      =   $_POST['reservation-email'];
        $phone      =   $_POST['reservation-phone'];
        $message    =   $_POST['reservation-message'];

        // Company email
        $master     =   'my@email.address';
        // Use a from address
        $header     =   "From: $email"."\r\n";

        // Try mailing off the reservation to the hotel
        if(mail($master,'Reservation Request',$message,$header)) {
            // If success, send another email to the customer
            $header =   "From: no-reply@hotel.com"."\r\n";
            mail($email,'Reservation Confirmation',$message,$header);
            // What is the difference between this "reservation_message" and "reservation-message"??
            $reservation_message    =   $_POST['reservation_message'];
            // Redirect back
            header('Location: '.$_POST['reservation-url'].'?message='.$reservation_message);
            exit;
        }
        else {
            echo 'Sorry, your reservation failed. Please contact customer service for reservations.';
            exit;
        }
    }
    ?>

【讨论】:

  • 嘿@Rasclatt 感谢代码。我用我的 reservation.php 文件中的代码替换了它,它抛出了同样的错误,只是这次在第 36 行(标题(位置)在哪里。有什么想法吗?
  • 您可以编辑您的帖子并从页面的最顶部往下添加大概 100 行吗?
  • 另外,这个页面是否包含在另一个页面中?这将影响整个场景。
  • 是的,reservation.php 文件在 wordpress 网站的“预订”页面上运行。我编辑了我的原始帖子以在 reservation.php 文件中显示整个代码。我已经发布了“预订”页面的代码here
  • 在页面的最顶部,&lt;?php 之前是否有空格?在您的示例中,您可以这样做。
【解决方案2】:

在对 Stack Overflow 进行一些研究后,我能够解决这个问题。

我在包含标题的行的最后一个字符串中添加了 urlencode。我做了:

Header('Location: '. $_POST['reservation-url'].'?message='.$reservation_message);

看起来像这样:

Header('Location: '. $_POST['reservation-url'].'?message='.urlencode($reservation_message));

为了启用邮件功能发送电子邮件,我更改了第 54 行

$headers = 'From: '. $name . '<' . $email . '>' . "\r\n" .

到这里

$headers = 'From: Booking@hotel.com' . "\r\n" .

&第 70 行来自

$to = $blog_email;

$to = 'my@email.address';

希望这将帮助任何陷入与我类似情况的人。非常感谢所有提供帮助的人。

【讨论】:

    猜你喜欢
    • 2013-04-25
    • 2013-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-15
    • 2011-12-17
    • 1970-01-01
    相关资源
    最近更新 更多