CakePHP“无法修改标头信息”问题不是空格
错误如下:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
640
是stripslashes_deep()
中foreach
循环的开始111
是debug()
在我看来,您正在代码中的某个位置调用
debug()
函数?在你的代码中是这样的:header -而不是=
640
is the start of aforeach
loop instripslashes_deep()
111
isdebug()
Appears to me you're calling the
debug()
function somewhere in your code?Aslo in your code is it:header -and not=
在您的例子中,您在第 111 行使用
echo
,即上面的“or from PHP”部分。由于这一行似乎来自核心包,我猜您正在调用一个以某种方式导致此输出的函数。您可以在第 111 行附近使用
debug_print_backtrace()
查看函数调用链导致echo
调用。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 theecho
call.不仅是空格,任何输出(例如您所做的
echo
)都会创建输出,从而阻止稍后使用header()
。您可以通过默认启用输出缓冲来解决此问题:
但前提是(意外)输出小于缓冲区大小。
另一种解决方法是检查标头是否已在发送标头之前发送。要检查的函数称为
headers_sent()
。因此,对于进行重定向的地方的解决方法(不知道这是否适用于面包店的蛋糕):Not only whitespace but any output, e.g. the
echo
you do, is creating output that will prevent usingheader()
later on.You can work around this be enabling output buffering by default:
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):