-1

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

Community
  • 1
  • 1
lollercoaster
  • 13,421
  • 28
  • 94
  • 162
  • 1
    i dont have the cake core in front of me, but what is on line 640? (and line 111) – Naftali aka Neal Jun 22 '11 at 15:57
  • @lollercoaster really quick - please paste the code near `/usr/share/php/cake/basics.php` at line-number 111??? – Rakesh Sankar Jun 22 '11 at 15:58
  • Make sure there's no byte-order-mark (BOM) in your UTF-8 files. Save them as UTF-8 without BOM. – Emil Vikström Jun 22 '11 at 15:59
  • possible duplicate of [Warning: Cannot modify header information](http://stackoverflow.com/questions/5675385/warning-cannot-modify-header-information) – mario Jun 22 '11 at 16:07
  • my texteditor removes BOM by default, and no, @mario, it is not. I saw that post before I posted here. mine is not a whitespace problem, so it is not a duplicate – lollercoaster Jun 22 '11 at 16:08
  • 2
    @lollercoaster: You can find a better suitable exact duplicate if you like. But with around 157238 of them there is no reason to keep this question open. A few thousand people made the mistake of outputting html tags before sending headers before you. (Also [the manual mentions exactly](http://php.net/header) that.) – mario Jun 22 '11 at 16:11
  • You are writing output using `echo` in line 111, which causes the headers being sent. You cannot use header redirection after using `echo` or `print`. – Ferdinand Beyer Jun 22 '11 at 16:13
  • @lollercoaster: I doubt that it's an error in CakePHP. You can add a call to `debug_print_backtrace()` near line 111 to see where this code is executed. http://www.php.net/debug_print_backtrace – Ferdinand Beyer Jun 22 '11 at 16:21
  • Use output buffering, set the default to the PHP.ini recommendation: `output_buffering = 4096`. Can eat at least the one or other little output. Increase it if you still run into errors. – hakre Jun 22 '11 at 16:26
  • Please do the following: (1) Go to line 111 of `cake/basics.php`. (2) Add the following line: `debug_print_backtrace();`. (3) Refresh the page. (4) Show us the output lines starting with `#0 debug() called at [...]`. – Ferdinand Beyer Jun 22 '11 at 16:30
  • @hakre: which one? for `locate php.ini` I get: /etc/php5/apache2/php.ini; /etc/php5/cli/php.ini; /usr/share/doc/php5-common/examples/php.ini-dist; /usr/share/doc/php5-common/examples/php.ini-paranoid; /usr/share/doc/php5-common/examples/php.ini-recommended; /usr/share/php5/php.ini-dist; /usr/share/php5/php.ini-dist.cli – lollercoaster Jun 22 '11 at 16:31
  • Loller, take the one of them that includes this setting, most likely the first one. – hakre Jun 22 '11 at 16:38

3 Answers3

2

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 =

Charles Sprayberry
  • 7,580
  • 2
  • 37
  • 49
  • I have not used the `debug()` function. so unless cake's default baking includes that method, then no I'm not calling it. hmmm – lollercoaster Jun 22 '11 at 16:11
  • I couldn't help you with the bake part of it. A lot of 'Cakers' out there would probably scoff at this but I've never baked. I'd much rather read through documentation and API and figure out how the shit works. – Charles Sprayberry Jun 22 '11 at 16:13
  • i'd say this is the answer.. reading the track i'd say that you're using a plugin that uses the debug() function... if not, you could do a text search on the app directory to find the debug() – pleasedontbelong Jun 22 '11 at 16:27
  • `grep -r "debug()" myappdirectory` returned no matches – lollercoaster Jun 22 '11 at 17:26
  • of course not, as the debug function takes an argument (a variable to debug) -- try grep -Ri "debug(" appdirectory – Abba Bryant Jun 22 '11 at 20:20
  • true. and good point. however I did the search the correct way and nothing turned up. – lollercoaster Jun 22 '11 at 23:37
0

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.

Ferdinand Beyer
  • 58,119
  • 13
  • 142
  • 141
  • ok..so should I comment out line 111? I'm wondering why I would be needing to alter CakePHP core files. Why would the makers of cake put in lines into the core that would break code like this?? – lollercoaster Jun 22 '11 at 16:19
  • line 111 is cakephp code.. he's actually using the debug() function somewhere in his code, that's what's causing this error.. – pleasedontbelong Jun 22 '11 at 16:21
  • I added a link to the output of debug_print_backtrace(), though it is quite long – lollercoaster Jun 22 '11 at 16:40
  • Well, your paste looks a bit like a chicken-and-egg problem, since `debug()` is called because of the PHP warning after `header()` fails. It seems that you actually captured a second call to `debug()`. Please put a `exit();` after `debug_print_backtrace();` and try again, to make sure you only get the correct backtrace. – Ferdinand Beyer Jun 22 '11 at 20:05
0

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;
hakre
  • 178,314
  • 47
  • 389
  • 754
  • but I didn't "do" that...it's part of cakephp internal core code! – lollercoaster Jun 22 '11 at 16:27
  • Show the code where you perform the redirect please. It's missing in your question. – hakre Jun 22 '11 at 16:36
  • it is in my above post: `$this->redirect(array('action'=>'index')); // line 25` in `posts_controller.php` – lollercoaster Jun 22 '11 at 16:41
  • Okay, have over-seen that line, was searching for header. Please [report this as a bug to the Cake PHP project](http://cakephp.lighthouseapp.com/dashboard), that HTTP redirects based on response headers are done even if header were already send. – hakre Jun 22 '11 at 16:49