-2

Possible Duplicate:
Headers already sent by PHP

I dunno but awhile ago, the redirect function is working. But now it gives me this error:

 Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\NewFolder\app\webroot\relationship1\app\controllers\books_controller.php:114) [CORE\cake\libs\controller\controller.php, line 744]

This is my controller: C:\xampp\htdocs\NewFolder\app\webroot\trial\app\controllers\users_controller.php

<?php
    class UsersController extends AppController{

        function register(){
            if(!empty($this->data)){
                if($this->User->save($this->data)){
                    $this->Session->setFlash('Data saved');
                    $this->redirect(array('action'=>'index'));
                } else{
                    $this->Session->setFlash('Could not save data');
                }
            } else{

                $this->Session->setFlash('Please fill this form');
            }
        }

        function index(){
            $users = $this->User->find('all');
            $this->set('users',$users);
        }

        function login(){
        //$this->Session->write('user','chams');
            //pr($this->Session->read());
            if(!empty($this->data)){
                //pr($this->data['User']['username']);
                $username = $this->data['User']['username'];
                //if($this->User->read(NULL,$username)){
                //$condition = array('User.username'=>$username);
                if($this->User->findByUserName($username)){
                    pr('existing');
                    $data = $this->User->find('first',$username);
                    $this->set('data',$data);
                    if($this->redirect(array('action'=>'main'))){
                        $this->Session->setFlash('redirected to index');
                    }

                } else{
                    pr('non existing');
                }

            }
        }
    }
?>
Community
  • 1
  • 1
Charmie
  • 2,468
  • 7
  • 33
  • 47
  • is your problem in login redirect? – thecodeparadox May 04 '12 at 08:18
  • exact duplicate. you cannot output anything to the screen prior to a redirect header... remove all closing `?>` from your php files! maybe it is a whitespace after one of them causing this. – mark May 04 '12 at 08:23

3 Answers3

0

I assume that $this->set('data',$data); fill up template, try remove it. You try to redirect user, so it is redundand anyway.

IProblemFactory
  • 9,551
  • 8
  • 50
  • 66
0

In your login you write

$this->set('data', $data);

before redirect, which already sent an header output. you should modify the login method:

.......    
if($this->User->findByUserName($username)){
   pr('existing'); // remove this
   $data = $this->User->find('first',$username);           
   if($this->redirect(array('action'=>'main'))){
      $this->Session->setFlash('redirected to index');
   }

   } else {
        pr('non existing');
   }
    $this->set('data',$data);
    .....
thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
0
<?php ob_start(); ?>

put this code in your ctp

Dilip Godhani
  • 2,065
  • 3
  • 18
  • 33