2012-05-04 47 views
-2

Possible Duplicate:
Headers already sent by PHPCakePHP的重定向不工作

我不知道,但前一段時間,重定向功能工作。但現在它給了我這個錯誤:

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] 

這是我的控制器:C:\ XAMPP \ htdocs中\ NewFolder \程序\ webroot的\試用\程序\控制器\ 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'); 
       } 

      } 
     } 
    } 
?> 
+0

在登錄你的問題重定向? – thecodeparadox

+0

重複。你不能在重定向頭之前輸出任何東西到屏幕上......從你的php文件中刪除所有關閉的'?>'!也許這是其中一個造成這種情況後的空白。 – mark

回答

0

我假設$this->set('data',$data);填滿模板,請嘗試刪除它。你嘗試重定向用戶,所以它是冗餘的。

0

在你登錄你寫

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

重定向之前,已經發送了頭輸出。你應該修改登錄方法:

.......  
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); 
    ..... 
0
<?php ob_start(); ?> 

把這個代碼在你的CTP