【问题标题】:CakePHP "Cannot modify header information" problem is NOT whitespaceCakePHP“无法修改标头信息”问题不是空格
【发布时间】:2011-06-22 15:55:42
【问题描述】:

这是错误:

Warning (2): Cannot modify header information - headers already sent by (output started at /usr/share/php/cake/basics.php:111) [CORE/cake/libs/controller/controller.php, line 640]

$status =   "Location: http://mydomain.com/blog/index"

header - [internal], line ??
Controller::header() - CORE/cake/libs/controller/controller.php, line 640
Controller::redirect() - CORE/cake/libs/controller/controller.php, line 621
PostsController::add() - APP/controllers/posts_controller.php, line 25
Object::dispatchMethod() - CORE/cake/libs/object.php, line 115
Dispatcher::_invoke() - CORE/cake/dispatcher.php, line 227
Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 194
[main] - APP/webroot/index.php, line 88

这是posts_controller.php的代码:

function add() {
    if (!empty($this->data)) {
        $this->Post->create();
        if ($this->Post->save($this->data)) {
            $this->Session->setFlash(__('Post saved!!', true));
            $this->redirect(array('action'=>'index')); // line 25
        } else {
        }
    }
    $tags = $this->Post->Tag->find('list');
    $statuses = $this->Post->Status->find('list');
    $this->set(compact('tags', 'statuses'));
}

这是第 111 行: echo "\n<pre class=\"cake-debug\">\n";

debug_print_backtrace()basics.php core cake 文件中的输出: http://pastebin.com/fBFrkYsP

我已经浏览了我编辑的所有文件(而不是我刚刚烘焙的文件),并且我在 php 括号 () 之外没有任何空格。我使用了这个脚本:Find all files with Blank or WS at BOF or EOF

我的文本编辑器设置为 UTF-8。基本上,当我注释掉第 25 行(上面标有注释)时,问题就消失了。但我应该能够使用重定向...谁能指出我正确的方向?

编辑: 在上面的 111 处添加了行;编辑 2:添加了 debug_print_backtrace() 的输出

【问题讨论】:

  • 我面前没有蛋糕芯,但是第640行是什么? (和第 111 行)
  • @lollercoaster 真的很快 - 请将代码粘贴到 /usr/share/php/cake/basics.php 附近的第 111 行???
  • 确保您的 UTF-8 文件中没有字节顺序标记 (BOM)。将它们保存为不带 BOM 的 UTF-8。
  • @lollercoaster:如果你愿意,你可以找到更合适的精确副本。但是其中大约有 157238 个,没有理由让这个问题保持开放。几千人在向您发送标头之前犯了输出 html 标签的错误。 (还有the manual mentions exactly那个。)

标签: php cakephp http-headers byte-order-mark


【解决方案1】:

640foreach 循环在stripslashes_deep() 中的开始

111debug()

在我看来,您在代码中的某处调用了 debug() 函数?

在你的代码中也是这样:

标题 -

而不是=

【讨论】:

  • 我没有使用debug()函数。所以除非蛋糕的默认烘焙包含该方法,否则我不会调用它。嗯
  • 烘焙部分我帮不了你。很多“蛋糕师”可能会对此嗤之以鼻,但我从来没有烤过。我宁愿通读文档和 API 并弄清楚这些东西是如何工作的。
  • 我会说这就是答案.. 阅读曲目我会说您正在使用使用 debug() 函数的插件...如果没有,您可以做一个文本在应用目录上搜索以找到 debug()
  • grep -r "debug()" myappdirectory 没有返回匹配项
  • 当然不是,因为调试函数需要一个参数(一个要调试的变量)——试试 grep -Ri "debug(" appdirectory
【解决方案2】:

请记住,必须在发送任何实际输出之前调用header(),无论是通过普通 HTML 标记、文件中的空白行还是通过 PHP。

在您的情况下,您在第 111 行使用echo,这是上面的“或来自 PHP”部分。

由于这一行似乎来自核心包,我猜你正在调用一个以某种方式导致此输出的函数。您可以在第 111 行附近使用 debug_print_backtrace() 查看导致 echo 调用的函数调用链。

【讨论】:

  • ok..所以我应该注释掉第 111 行吗?我想知道为什么我需要更改 CakePHP 核心文件。为什么蛋糕制造商会在内核中插入会破坏这样代码的代码??
  • 第 111 行是 cakephp 代码。他实际上在代码中的某处使用了 debug() 函数,这就是导致此错误的原因。
  • 我添加了一个指向 debug_print_backtrace() 输出的链接,虽然它很长
  • 好吧,你的粘贴看起来有点像鸡和蛋的问题,因为debug() 被调用是因为header() 失败后的PHP 警告。看来您实际上捕获了对debug() 的第二次呼叫。请在debug_print_backtrace(); 之后添加exit(); 并重试,以确保您只获得正确的回溯。
【解决方案3】:

不仅是空格,还有任何输出,例如您所做的 echo 正在创建将阻止以后使用 header() 的输出。

您可以通过默认启用output buffering 来解决此问题:

; output_buffering
;   Default Value: Off
;   Development Value: 4096
;   Production Value: 4096

output_buffering = 4096

但仅当(意外)输出小于缓冲区大小时。

另一种解决方法是检查是否在发送标头之前已发送标头。要检查的函数称为headers_sent()。因此,对于进行重定向的地方的解决方法(不知道这是否适用于面包店的蛋糕):

$url = 'http://absolut.http/uri';
if (!headers_sent()) {
   header('Location: '.$url, 301);
} else {
   printf('<meta http-equiv="Location" content="%s>"', $url);
}
printf('<h1><a href="%s">Moved.</a></h1>', $url);
exit;

【讨论】:

  • 但我没有“这样做”...它是 cakephp 内部核心代码的一部分!
  • 请显示您执行重定向的代码。您的问题中缺少它。
  • 它在我上面的帖子中:$this-&gt;redirect(array('action'=&gt;'index')); // line 25 in posts_controller.php
  • 好的,已经看过那行,正在搜索标题。请report this as a bug to the Cake PHP project,即使标头已经发送,基于响应标头的 HTTP 重定向也会完成。
猜你喜欢
  • 2013-10-03
  • 1970-01-01
  • 1970-01-01
  • 2011-11-14
  • 2021-11-15
  • 2017-10-30
  • 1970-01-01
  • 1970-01-01
  • 2011-11-14
相关资源
最近更新 更多