gpt4 book ai didi

php - 警告 : Cannot modify header information - headers already sent by

转载 作者:行者123 更新时间:2023-12-04 06:50:05 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





How to fix "Headers already sent" error in PHP

(11 个回答)



Reference - What does this error mean in PHP?

(37 个回答)


9年前关闭。




请救救我。我知道这个问题已经被问过很多次了,但是,我似乎无法找到与我的情况相关的解决方案。

问题:Warning: Cannot modify header information - headers already sent by (output started at /.../Sites/st.ambulance/resources/views/login.view.php:32) in /.../Sites/st.ambulance/resources/controllers/tables_.php on line 47
这是第 32 行的代码块:

      <label>Month</label>
<select name="dob_month">
<?php for($i=0,$j=1;$i<sizeof($month);$i++,$j++){ ?>
<option value="<?php h($j) ?>"><?php h($month[$i]) ?></option> //line 32
<?php } ?>
</select>

h函数的定义:
function h($s){
echo(htmlspecialchars($s,ENT_QUOTES));
}

这是tables_.php:
<?php
$error = "";
if(isset($_POST['guest_tables'])){
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in']){
$guest = array();
$volunteer = array();

$guest = isset($_POST['guest']) ? $_POST['guest'] : null;
$volunteer = isset($_POST['volunteer']) ? $_POST['volunteer'] : null;

$seat_array = array($volunteer['seat_no'].$volunteer['table']);
$seat_count = 0;
$table_seat_error = "";

if($form->is_seatOccupied($volunteer['table'],$volunteer['seat_no']) != "")
$table_seat_error .= "Seat (".$volunteer['seat_no'].")
at table (".$volunteer['table'].") is currently occupied";

if($_SESSION['no_guests'] >= 1){
if($guest && $volunteer){
foreach($guest as $gue){

$seat_table = $gue['seat_no'].$gue['table'];
for($h=0;$h<sizeof($seat_array);$h++){
if($seat_table == $seat_array[$h] )
$seat_count = $seat_count + 1;
}
if($form->is_seatOccupied($gue['table'], $gue['seat_no']) != "")
$table_seat_error .= "Seat (".$gue['seat_no'].")
at table (".$gue['table'].") is currently occupied";

$seat_array[] = $seat_table;
}
if($seat_count == 0){
if($table_seat_error == ""){
for($d=0;$d<$_SESSION['no_guests'];$d++){
$_SESSION['guests'][$d]['table'] = $guest[$d]['table'];
$_SESSION['guests'][$d]['seat'] = $guest[$d]['seat_no'];
}

$_SESSION['volunteer']['table'] = $volunteer['table'];
$_SESSION['volunteer']['seat'] = $volunteer['seat_no'];

$form->set_guests($_SESSION['guests']);
$form->set_volunteer($_SESSION['volunteer']);

header('location: /branch/menus.php'); //line 47
exit();
}
else{
$error = $table_seat_error;
}
}
else{
$error = "You have selected the same seat for two or more
people: one person, per seat, per table. Only.";
}
}
}
else{

$_SESSION['volunteer']['table'] = $volunteer['table'];
$_SESSION['volunteer']['seat'] = $volunteer['seat_no'];

if(!$form->is_seatOccupied($_SESSION['volunteer']['table'],
$_SESSION['volunteer']['seat']) != ""){
$form->set_volunteer($_SESSION['volunteer']);

header('location: /branch/menus.php');
exit();

}
}
}
}
?>

编辑:知道我正在尝试在一个页面上处理多个表单会有所帮助吗?

最佳答案

第 47 行修改了响应头:

header('location: /branch/menus.php'); //line 47

第 47 行无法执行,因为 header 已经发送,而这恰好发生在第 32 行。第 32 行发生的所有事情是 PHP 认为响应中有足够的内容开始将其发送回浏览器。

一般来说,如果你想修改标题(无论你在第 47 行做什么), 你需要在文件的开头做 .

这是了解 MVC design pattern 的好时机- 它将消除将来出现此问题的可能性。在 MVC 中,Controller 首先执行,并为 View 准备好一切。因此,您可以在 Controller 中修改您想要的所有标题,并且在处理 View 期间或之后它们不会被发送。

编辑 :看起来您正在使用 MVC,但不知何故您的 View 在 Controller 启动(或可能完成)之前执行......这不应该发生!不幸的是,您发布的代码并未说明如何访问 Controller 或 View ......但它们被无序访问。

正如 TRiG 在下面的评论中指出的那样,您可能想要插入 exit()第 47 行之后的语句如下:
header('location: /branch/menus.php'); //line 47
exit();

这将导致服务器立即向浏览器发送位置重定向,请求/响应周期完成。你所有的 header('Location: ...');调用后应紧跟 exit(); .这并不能解决手头的问题,但它非常重要。

关于php - 警告 : Cannot modify header information - headers already sent by ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3260600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com