-1

这是错误:

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()的输出:http: //pastebin.com/fBFrkYsPbasics.php

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

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

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

4

3 回答 3

2

640foreach循环的开始stripslashes_deep()

111debug()

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

Aslo 在您的代码中是:

标题 -

并不是=

于 2011-06-22T16:07:22.197 回答
0

不仅空格,而且任何输出,例如echo你所做的,都会创建将阻止header()以后使用的输出。

您可以通过默认启用输出缓冲来解决此问题:

; 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;
于 2011-06-22T16:23:30.493 回答
0

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

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

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

于 2011-06-22T16:16:33.623 回答