CakePHP“无法修改标头信息”问题不是空格

发布于 2024-11-16 10:58:04 字数 1933 浏览 0 评论 0原文

错误如下:

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

\n";

basics.phpdebug_print_backtrace() 的输出> 核心蛋糕文件: http://pastebin.com/fBFrkYsP

我已经浏览了我编辑过的所有文件(而不是我刚刚烘烤的)并且我在 php 括号()之外没有任何空格。我使用了这个脚本:查找所有带有空白的文件或 WS 位于 BOF 或 EOF

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

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

Here's the error:

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

Here's the code for 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'));
}

Here's line 111:
echo "\n<pre class=\"cake-debug\">\n";

Output of debug_print_backtrace() in basics.php core cake file:
http://pastebin.com/fBFrkYsP

I've got through all the files I have edited (as opposed to ones I just baked) and I there isn't any whitespace outside of the php brackets (). I used this script: Find all files with Blank or WS at BOF or EOF.

My text editor is set to UTF-8. Basically the problem goes away when I comment out line 25 (marked above with comment). But I should be able to use a redirect...can anyone point me in the right direction?

EDIT:
added line at 111 above; EDIT 2: added output of debug_print_backtrace()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一世旳自豪 2024-11-23 10:58:04

640stripslashes_deep()foreach 循环的开始

111debug()

在我看来,您正在代码中的某个位置调用 debug() 函数?

在你的代码中是这样的:

header -

而不是=

640 is the start of a foreach loop in stripslashes_deep()

111 is debug()

Appears to me you're calling the debug() function somewhere in your code?

Aslo in your code is it:

header -

and not =

錯遇了你 2024-11-23 10:58:04

请记住,header() 必须在任何实际输出之前调用通过正常的 HTML 标签、文件中的空行或从 PHP 发送。

在您的例子中,您在第 111 行使用 echo,即上面的“or from PHP”部分。

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

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

In your case, you are using echo on line 111, which is the "or from PHP" part above.

Since this line appears to be from the core package, I guess you are calling a function that somehow causes this output. You can use debug_print_backtrace() near line 111 to see the chain of function calls leading to the echo call.

冷血 2024-11-23 10:58:04

不仅是空格,任何输出(例如您所做的 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;

Not only whitespace but any output, e.g. the echo you do, is creating output that will prevent using header() later on.

You can work around this be enabling output buffering by default:

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

output_buffering = 4096

But only if the (accidental) output is less than the buffer size.

Another workaround is to check if headers have been send prior sending headers. The function to check is called headers_sent(). So for a workaround at the place of doing the redirect (no idea if that's applicable for the cakes in the bakery):

$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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文