警告:标头不能包含多个标头,检测到新行


Warning: Header may not contain more than a single header, new line detected

我有一个关于PHP错误的问题。我研究了其他类似的问题,并应用了它,但我仍然有这样的问题,所以我问它。

情况是这样的。有一个文本框叫做特殊指令;如果我只放一行就可以了,但如果我放了不止一行就会引起这样的问题。我放了urlencode(str)来解决这个问题,但它仍然存在。我需要更改什么?T.T

错误消息是"警告:标头可能不包含超过一个标头,检测到新行。"

if($_POST['Submit'] == 'Submit Order'){
    $today = date('D M dY');
    $to_time = date('h:i a');
    $hours = substr($to_time, 0, -6);
    $minutes = substr($to_time, -5, 2);
    $seconds = substr($to_time, -2);
    $total_seconds = ($hours * 3600) + ($minutes * 60) + $seconds;
    $date123 = $_POST['day'];
    $date_post = date('D M dY',$date123);
    $time_post = $_POST['time']; 
    $hours_post   = substr($time_post, 0, -6);
    $minutes_post = substr($time_post, -5, 2);
    $seconds_post = substr($time_post, -2);
    $total_seconds_post = ($hours_post * 3600) + ($minutes_post * 60) + $seconds_post;
    $tip_amo = $_POST["tip_amount"];
    $deliv_mails = $_POST["deliv_mails"];
    $coupon_no = $_POST['coupon_no'];
    $minorder = $_POST['minorder'];
    $subtotal_amount = $_POST['subtotal_amount'];
    $payment_type = $_POST['payment_type'];
    $comments = addslashes($_POST['comments']);
    $coupon_code = $_REQUEST["coupon_code"];
    $customer_comments = get_field(CART,"additional_text","session_id='$sessionid'");
    if($comments == ''){
        $comments1 = $customer_comments;
    }else{
        $comments1 = $comments;
    }
    if($time_post!='') {
        if($today==$date_post){
            $query = [
                'day' => $date123,
                'time' => $time_post,
                'coupon_no' => $coupon_no,
                'minorder' => $minorder,
                'subtotal_amount' => $subtotal_amount,
                'payment_type' => $payment_type,
                'comments' => $comments1,
                'coupon_code' => $coupon_code,
                'tip_amount' => $tip_amo,
                'deliv_mails' => $deliv_mails,
                'd' => $address
            ];
            header('Location: details_info_order.php?' . http_build_query($query));
            exit;
        }else{
            $query = [
                'day' => $date123,
                'time' => $time_post,
                'coupon_no' => $coupon_no,
                'minorder' => $minorder,
                'subtotal_amount' => $subtotal_amount,
                'payment_type' => $payment_type,
                'comments' => $comments1,
                'coupon_code' => $coupon_code,
                'tip_amount' => $tip_amo,
                'deliv_mails' => $deliv_mails,
                'd' => $address
            ];
            header('Location: details_info_order.php?' . http_build_query($query));
            exit;
        }
    }
}

警告:标头不能包含多个标头,检测到新行。在第64行上的/home4/mikehan2/public_html/mikehan7/final_checkout_block.php中

这正是错误消息。

您真的应该使用类似http_build_query()的东西;无需手动进行URL编码,也无需混乱的字符串连接,例如

$query = array(
    'day' => $date123,
    'time' => $time_post,
    // etc
);
header('Location: details_info_order.php?' . http_build_query($query));
exit;

有些变量包含多行文本,可能是COMMENTS。使用urlencode:

'comments' => urlencode($comments1),
...