4
            function new_photo()
            {

                if( !empty($this->data))
                {
                        $this->data['Photo']['showcase_id'] = $this->Session->read('CurrShowcase.id');
                        $this->data['Photo']['added'] = date("Y-m-d H:i:s");
                        $this->Showcase->Photo->save($this->data);

                        $flasher = 'Photo uploaded successfully';
                        $flasher .= '<br/><img src="' . $this->data['Photo']['thumbnail_url'] . '"/>';
                        $this->Session->setFlash($flasher);
                        //$this->redirect(array('action'=>'sc',));
                }
            }   

I have a Showcase Controller in my CakePHP app, and a new photo form to submit new photos. Whenever I uncomment the last line that redirects after the data is saved, I get this error:

Warning (2): Cannot modify header information - headers already
sent by (output started at D:\.....

Even if I get this error, $this->data still gets saved properly in the database. However, if I comment the redirect line as shown above, everything works fine and error-free. I HAVE checked for blank spaces around the tags, so I'm pretty sure it's not that.

Any ideas?

Edit: commenting out the setFlash statement does not fix the problem.

vette982
  • 4,782
  • 8
  • 34
  • 41
  • Have you tried to redirect to another action? Why would you not remove the comma after 'sc'? – bancer Jun 21 '10 at 23:19
  • The comma was one of the problems. I actually had a print_r($this->Session->read('CurrUser.username')); print_r($this->Session->read('CurrShowcase.id')); before the if statement which caused some issues. – vette982 Jun 23 '10 at 04:00

6 Answers6

3

Change your debug mode to 0 to make sure it's not a notice/warning being generated prior to the redirect. Also, you might want to tighten up your processing section to (be paranoid and) ensure that it's not using invalid indexes, as well as anywhere else throughout the application flow for that action to make sure you're not getting any ouput (if it works when you change debug to 0).

mway
  • 4,334
  • 3
  • 26
  • 37
1

Is there a debug statement somewhere that you're not showing us?

Leo
  • 6,553
  • 2
  • 29
  • 48
1

You may be up against an invisible UTF-8 BOM character somewhere. Check your text editor settings whether it saves your files with BOM or without.

Community
  • 1
  • 1
deceze
  • 510,633
  • 85
  • 743
  • 889
1

I'd check for whitespace in the models. Anyone of them. That was one of the gotchas I hit.

Nigel
  • 1,695
  • 1
  • 13
  • 22
0

I'm assuming setFlash outputs something to the browser?

If whitespace before or after your <?php ?> tags isn't your issue you might have to try passing 'null' for the 'layout' parameter of setFlash();

i.e.

$this->Session->setFlash($flasher, null); 
Matt Mitchell
  • 40,943
  • 35
  • 118
  • 185
0

Either this code outputs something to the browser, or you have a whitespace after ?> in the end of the file (or any other included file). The whitespace is sent to the user thus sending http header.

cypher
  • 6,822
  • 4
  • 31
  • 48