2

I got this error message when i run my php CodeIgniter project:

A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at C:\AppServ\www\tree\application\models\mtree.php:17)

Filename: core/Common.php

Line Number: 442

line 17:

echo $row->members_id . "</br>";

this is my model function:

function convert_all() {
    $this->db->empty_table('table1');
    $this->db->empty_table('table2');
    $this->db->query("INSERT INTO `table1` (`members_id`, `members_username`);
    $query = $this->db->get('table2');
    foreach ($query->result() as $row) {
        if ($row->members_id > 1) {
            echo $row->members_id . "</br>";
        }
        if ($this->isadvanced($row->members_id, $row->members_direct_id)) {
            $this->insert_to_right($row, $row->members_direct_id);
        } else {
            $this->insert_to_left($row, $row->members_direct_id);
        }
    }
}
YakovL
  • 7,557
  • 12
  • 62
  • 102
user2988211
  • 69
  • 3
  • 4
  • 9
  • 3
    Unrelated: You're missing quotes at the end of this statement: *$this->db->query("INSERT INTO `table1` (`members_id`, `members_username`);* – Amal Murali Nov 20 '13 at 12:34
  • Error caused by some content being sent to the user **before the call** to `header` function. The code you gave does not provide enough information to aid debugging. Read more about the error: http://php.net/manual/en/function.header.php – adeelx Nov 20 '13 at 12:37
  • Take a look here http://stackoverflow.com/questions/8028957/headers-already-sent-by-php – Suvash sarker Nov 20 '13 at 12:39
  • thanks shuvo for helping – user2988211 Nov 20 '13 at 12:54

4 Answers4

12

While using codeigniter, its best recommended that you echo only in views.

Echoing anywhere randomly sends some data to browser, after which you can't modify headers (this includes attempt to redirect, set content-type, etc)

You should re-organize your code, such that, echo are done within views only. This will solve your header issues in Codeigniter.

This also includes extra whitespaces at the end after the closing brace "} ?>" in non-view php files.

linuxeasy
  • 6,269
  • 7
  • 33
  • 40
1

You can't display anything before the header function is called. You should not display anything form model or controller to avoid this kind of error. If you want to display something form the controller or model then you should store the output to the buffer instead of sending it to the browser. You can store the output to the buffer using ob_start() function before header() function is called. Then you can do something like this

if(true)
{
    header("Location:http://google.com");
}
else
{
   ob_end_flush();
}
YakovL
  • 7,557
  • 12
  • 62
  • 102
Monir
  • 115
  • 1
  • 7
1

I think it will be helpfull Use $config['sess_save_path'] = sys_get_temp_dir(); instead of $config['sess_save_path'] = NULL; in your Application/config/config.php file

Lundin
  • 195,001
  • 40
  • 254
  • 396
MALIK KHAN
  • 41
  • 1
0

this was your issue - echo $row->members_id . ""; -- as the above answers say, don't echo something in your controllers, push to do it in the views - always

joemisika
  • 74
  • 1
  • 5