Can not modify header information - headers already sent ... in Codeigniter

1

How about, I'm inserting data and an image in Codeigniter by means of a form, all that works, but it sends me the following:     

Severity: Warning

Message: Can not modify header information - headers already sent by (output started at /xxx/xxx/xxxx/xxxx/application/controllers/MController.php:78)

Filename: helpers / url_helper.php

Line Number: 564

Backtrace:

File: /xxx/xxxx/xxxxx/xxxx/application/models/MModel.php Line: 124 Function: redirect

File: /xxx/xxx/xxx/xxxx/application/controllers/MController.php Line: 94 Function: insert Test

The error you flag is in MController on line 78 ,

public function add(){


         $config['upload_path'] = './imagenes/';
         $config['allowed_types'] = 'gif|jpg|png|jpeg';
         $config.... 
  {
  echo("<script>console.log('Error al subir la imagen');</script>");
  $error = array('error' => $this->upload->display_errors());
  }
  else
  {

   echo("<script>console.log('Imagen subida correctamente');</script>");// <---Aquí marca error, línea 78

  }

In MModel the line that marks error is the 124 :

public function insertPrueba($data){

                $this->db->insert(TABLE_PRODUCTO,$data);
              redirect('MController');// Aquí marca el error

        }

In index () of MController I load some data that I want to show and load static pages:

public function index()
 {




        $data['me'] = $this->MModel->getAll();
        $data['mB'] = $this->MModel->getAllB();
        $data['mP'] = $this->MModel->getAllP();
    $data['cat'] = $this->MModel->getAllCat();


        $this->load->view('estaticos/header');
        $this->load->view('dinamicos/menu/menus', $data);
        $this->load->view('estaticos/menu_izquierdo');
        $this->load->view('estaticos/footer');
        $this->load->view('estaticos/menu_derecho');
    }

I do not know if I have anything to do with using redirect('MController'); ...

I have reviewed these answers, but I do not have blank spaces starting <?php or at the end ... or anything of the possible causes .. How to fix "Headers already sent" error in PHP

    
asked by x4mp73r 07.06.2016 в 19:09
source

1 answer

0

CodeIgniter does the redirection through the header / headers ( header ), but once it has been written on the page, it is not possible to change the values of the header. That is why you are receiving the error, as specified in the documentation (in English) ( my translation):

  

Note: for this function to work properly, it must be used before something is written to the browser because it uses the server headers

In your code, you are performing an operation and there is a if..else that has a echo in both branches. That means that something is always going to be written on the screen. By putting the redirect after those operations, you will always receive the error message you mention.

    
answered by 07.06.2016 / 19:20
source