【问题标题】:trouble in using include and header in php. "Cannot modify header information" [duplicate]在 php 中使用 include 和 header 的麻烦。 “无法修改标头信息” [重复]
【发布时间】:2013-04-12 00:49:44
【问题描述】:

我有一个文件“header.php”,其中有一个搜索文本框。另一个名为“myPage.php”的文件包括“header.php”。 在 header.php 中

 if (isset($_POST['Searchbutton'])){ // this will execute when the search button is clicked.
 $target = $_POST['searchtext'];
 header("Location: searchresult.php?text=" . $target); // line #8
 }

在 myPage.php 中

<?php
include("header.php");
?>

当我在“myPage.php”上并使用搜索选项时,我收到一条错误提示

Cannot modify header information - headers already sent by (output started at myPage.php:3) in header.php on line 8.

谁能帮我理解这个概念。我是php的新手。请询问是否需要更多信息。

干杯!

【问题讨论】:

  • 在使用header()之前不要输出任何东西

标签: php header include


【解决方案1】:

header() 如果已经输出到浏览器,则无法调用。您可以在代码开头使用ob_start() 避免此问题,然后在您要发送输出的位置使用ob_flush()。这将缓冲所有输出到浏览器,直到调用ob_flush()

这也意味着 header() 重定向,或者其他更改只要发生在ob_flush()之前就可以进行

【讨论】:

  • 在这种情况下没有理由缓冲输出。
【解决方案2】:

简而言之,您尝试做的事情没有意义。当您发送一个页面时,您也发送了 HTTP 标头。您正在尝试发送一个页面,然后发送一个将客户端重定向到另一个页面的标头。

这就是为什么您收到标头已发送错误的原因。由于您只是尝试发布表单,因此没有理由尝试重定向...只需将表单的目标设置为处理搜索条件的脚本即可。

【讨论】:

    【解决方案3】:

    在发送 header() 之前,您不能向浏览器输出内容。只要看看http是如何工作的,你就会明白:)

    http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Client_request

    【讨论】:

      猜你喜欢
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 2011-08-03
      • 2012-06-19
      • 1970-01-01
      相关资源
      最近更新 更多