php - 收到警告标题在wordpress上不得包含多个标题

标签 php wordpress error-handling

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

当尝试以表格形式输入数据时,我在第6行收到一条PHP错误警告,提示“ header 不能包含多个 header ”。我尝试了诸如检查URL之类与PHP无关的解决方案。我在这里阅读了一些依赖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类(class)(如果免费,则很棒),我也将不胜感激。

谢谢,

阿迪

最佳答案

此代码段的整个问题是您有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;
        }
    }
    ?>

关于php - 收到警告标题在wordpress上不得包含多个标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29618304/

相关文章:

php - 添加样式回显

html - 如何在wordpress上预加载字体?

php - 在 wordpress 站点中安装 ssl 后,Authorize.net 无法正常工作

php - 在 php 中获取具有相同用户 ID 的所有行值?

scala - 在 Scala 中,如何递归地在单个 Some trait 中构造一个列表?

php - 如何在相同按钮位置的中心动态对齐单选按钮?

php static::inside 匿名函数

php - cakephp 2.0 中的 LdapAuth

java - 需要添加 “public static void main(String[] args)”,

error-handling - 如何在我自己的代码中构造一个 ParseIntError?