【问题标题】:Cakephp redirect is not working [duplicate]Cakephp重定向不起作用[重复]
【发布时间】:2012-05-04 08:15:44
【问题描述】:

可能重复:
Headers already sent by PHP

我不知道,但不久前,重定向功能正在工作。但现在它给了我这个错误:

 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\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');
                }

            }
        }
    }
?>

【问题讨论】:

  • 你的登录重定向有问题吗?
  • 完全重复。您无法在重定向标头之前向屏幕输出任何内容...从您的 php 文件中删除所有关闭的?&gt;!可能是其中一个导致此问题之后的空格。

标签: php cakephp redirect


【解决方案1】:

我假设$this-&gt;set('data',$data); 填满了模板,请尝试删除它。您尝试重定向用户,因此无论如何它都是多余的。

【讨论】:

    【解决方案2】:

    在你的登录中你写

    $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);
        .....
    

    【讨论】:

      【解决方案3】:
      <?php ob_start(); ?>
      

      将此代码放入您的 ctp

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-05-05
        • 1970-01-01
        • 1970-01-01
        • 2020-04-05
        • 1970-01-01
        • 2010-09-30
        • 1970-01-01
        相关资源
        最近更新 更多