开发者

CakePHP "Cannot modify header information" problem is NOT whitespace

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()


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 =


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.


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜