Php 获取警告标题在wordpress上不能包含多个标题

Php 获取警告标题在wordpress上不能包含多个标题,php,wordpress,error-handling,Php,Wordpress,Error Handling,我正在学习WordPress和PHP,并在www.sunriseLotting.com上建立一个网站 当试图在表单中输入数据时,我在第6行收到一条PHP错误警告,说“header不能包含多个header”。我尝试过一些解决方案,比如检查与PHP无关的URL。我在这里读到了一些依赖于PHP的解决方案,因此我不知道这里需要做什么 我的代码如下: <?php $reservation_message = $_POST['reservation_message']; /

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

当试图在表单中输入数据时,我在第6行收到一条PHP错误警告,说“header不能包含多个header”。我尝试过一些解决方案,比如检查与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位于此处-。显然,表格也没有发送电子邮件。有人能看一下这个并告诉我这里的代码有什么问题吗

如果有人能指导我学习一门适合初学者的在线PHP课程,我也会很感激的(如果免费的话,那就太好了)

谢谢,


Aadi

这个snipp的全部问题在于1)没有条件2)没有
mail()
函数

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

<?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;
        }
    }
    ?>

在对堆栈溢出进行了一些研究之后,我能够解决这个问题

我在包含标题的行的最后一个字符串中添加了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" .
&第70行来自

$to = $blog_email;


希望这将有助于任何人谁陷入了类似的情况我。非常感谢所有帮助过你的人。

标题位置后的任何代码都不会run@aadi头后的代码不会运行。您应该始终在头之后放置并退出()或die(),以防止进一步执行代码。感谢您的响应。我对PHP完全陌生,无法找到解决方案。您能告诉我如何使用该代码使其工作吗?@aadi您应该广告“退出;”在你的头球之后。我不确定这是否重要,但我认为“Header”应该低一点。谢谢@bassxzero,我尝试添加了两个exit();退出;现在它给出了一个“(T_CONSTANT_ENCAPSED_STRING)”错误。我该怎么办?hey@Rasclatt谢谢你的代码。我用reservation.php文件中的代码替换了它,它抛出了相同的错误,只是这次在第36行(标题(位置)处)是的。有什么想法吗?你能编辑你的帖子并从页面顶部向下添加100行吗?还有,这个页面是否包含在另一个页面中?这将影响整个场景。是的,reservation.php文件运行在wordpress网站的“reservation”页面上。我编辑了我的原始帖子,以在reservation.ph中显示整个代码p文件。我已经在页面顶部发布了“预订”页面的代码,在

$to = $blog_email;
$to = 'my@email.address';