收到警告Header在wordpress上可能不包含多个标题

时间:2015-04-14 02:42:33

标签: php wordpress error-handling

我正在学习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();

?>

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

如果有人能指导我为初学者提供一个优秀的在线PHP课程(如果免费的话很棒的话),我也很感激。

谢谢,

AADI

2 个答案:

答案 0 :(得分:0)

这里snippit的全部问题在于你有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;
        }
    }
    ?>

答案 1 :(得分:0)

在对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';

希望这能帮助那些陷入与我相似的人。非常感谢所有帮助过的人。