getting warning Header may not contain more than a single header on wordpress

I’m learning WordPress and PHP and building a website at www.sunriselodging.com

When trying to enter data in a form, I get a PHP error warning saying ‘header may not contain more than a single header’ on line 6. I’ve tried solutions like checking the URL which are not related to PHP. I read a few solutions here that are PHP dependent and hence I cannot figure out what needs to be done here.

Read More

My code is below:

    <?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 . "rn" .
        'Reply-To: '. $reply . "rn" .
        '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 . '>' . "rn" .
    'Reply-To: '. $name . '<' . $email . '>' . "rn" .
    '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();

?>

The URL for the form is here – http://www.sunriselodging.com/reservation/. Apparently the form hasn’t been sending emails either. Can someone please look at this and advise what’s wrong with the code here?

I’ll also appreciate if someone can direct me towards a good online PHP course for beginners (great if free).

Thank You,

Aadi

Related posts

Leave a Reply

2 comments

  1. The whole problem with this here snippit is that you have 1) no conditions 2) no mail() function.

    If you are going to use a header, you should probably have a condition. You should also probably have a condition to execute the emailer. Finally, you need to run this code before everything on the page (likely before the get_header() if on the page). Your workflow should go something similar to this:

    <?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"."rn";
    
            // 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"."rn";
                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;
            }
        }
        ?>
    
  2. I was able to solve this after some research on Stack Overflow.

    I added urlencode to the last string on the line containing header. I made:

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

    to look like this:

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

    To enable the mail function to send email I changed line 54 from

    $headers = 'From: '. $name . '<' . $email . '>' . "rn" .
    

    to this

    $headers = 'From: Booking@hotel.com' . "rn" .
    

    & line 70 from

    $to = $blog_email;
    

    to

    $to = 'my@email.address';
    

    Hope this will help anyone who’s stuck in a situation similar to mine. A big THANK YOU to all the people who helped.