CodeIgniter Forums
Cannot modify header information - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Cannot modify header information (/thread-13398.html)



Cannot modify header information - El Forum - 11-20-2008

[eluser]Nathan Payne[/eluser]
http://www.haunted-houses.net/admin

I am getting the following error:

Quote:A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /home/phpwhizn/public_html/haunted-houses.net/system/application/libraries/Layout.php:37)

Filename: libraries/Session.php

Line Number: 662

This happens when I try to access the admin section of my sites. I have attached the code for my admin section below.

Controller:
Code:
<?php
    
    class Admin extends Controller{
        
    function Admin()
    {
        parent::Controller();
        $this->load->library('session');
    }
    
    function index()
    {
        $this->load->model('admin_model', '', TRUE);
        
        $this->layout->view('login');
    }
    
    function verify()
    {
        if ($this->input->post('username'))
        {
            $username = $this->input->post('username');
            $password = $this->input->post('password');
            
            $this->admin_model->authorize($username, $password);
        }
            $this->layout->view('login');
        
    }}

Model:
Code:
<?php

    class Admin_model extends Model{
        
        function Admin_model(){
            parent::Model();
        }
        
        function authorize($username, $password){
            $data = array();
            $this->db->select('id, username');
            $this->db->where('username', $username);
            $this->db->where('password', $password);
            $this->db->limit(1);
            
            $q = $this->db->get('users');
            if ($q->num_rows() > 0){
                $row = $q->row_array();
                $_SESSION['userid'] = $row['id'];
                $_SESSION['username'] = $row['username'];
                redirect('admin/dashboard','refresh');

            }else{
                $this->session->set_flashdata('error', 'Your login details failed!');
            }
        }
                
        }

View:
Code:
<div id="content">
    <h1>Admin Login</h1>
    &lt;?php if ($this->session->flashdata('error'))
    {
        echo $this->session->flashdata('error');
    }
    ?&gt;
    &lt;?php
    $username = array('name' => 'username', 'id' => 'username', 'size' => 20);
    $password = array('name' => 'password', 'id' => 'password', 'size' => 20);
    
    echo form_open("admin/verify");
    echo "<p><label for='username'>Username</label><br />";
    echo form_input($username) . "</p>";
    echo "<p><label for='password'>Password</label><br />";
    echo form_password($password) . "</p>";
    echo form_submit('submit', 'login');
    echo form_close();
    ?&gt;
    
    
</div></div>

<div id="sidebar">
    <div id="latesthouse">
        <h1>Latest Houses</h1>
        <h2>hb</h2>
            <p class="latesthouse"><img class="latesthouse" src="&lt;?php echo base_url().'images/picture.jpg'; ?&gt;"
        alt="House" />rdtfyvguhinjo
        
        </p>
    </div>

Also when I submit the form I get the following:
Quote:Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /home/phpwhizn/public_html/haunted-houses.net/system/application/libraries/Layout.php:37)

Filename: helpers/url_helper.php

Line Number: 528



Cannot modify header information - El Forum - 11-20-2008

[eluser]Armchair Samurai[/eluser]
Perhaps this is far too obvious a question, but what's on line 37 in Layout.php? From the errors, sounds like it's outputting something to the browser.


Cannot modify header information - El Forum - 11-20-2008

[eluser]Nathan Payne[/eluser]
[quote author="Armchair Samurai" date="1227253725"]Perhaps this is far too obvious a question, but what's on line 37 in Layout.php? From the errors, sounds like it's outputting something to the browser.[/quote]
Layout.php is below.
I have hunted the files for blank space etc.
Code:
&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Layout
{
    
    var $obj;
    var $layout;
    
    function Layout($layout = "layout_main")
    {
        $this->obj =& get_instance();
        $this->layout = $layout;
    }

    function setLayout($layout)
    {
      $this->layout = $layout;
    }
    
    function view($view, $data=null, $return=false)
    {
        $loadedData = array();
        $loadedData['content_for_layout'] = $this->obj->load->view($view,$data,true);
        
        if($return)
        {
            $output = $this->obj->load->view($this->layout, $loadedData, true);
            return $output;
        }
        else
        {
            $this->obj->load->view($this->layout, $loadedData, false);
        }
    }
}



Cannot modify header information - El Forum - 11-21-2008

[eluser]Nathan Payne[/eluser]
Can anyone help?


Cannot modify header information - El Forum - 11-21-2008

[eluser]Bogdan Tanase[/eluser]
if this line executes before redirect, then it won't work, because it will output headers to the browsers.

Code:
$this->obj->load->view($this->layout, $loadedData, false);



Cannot modify header information - El Forum - 11-21-2008

[eluser]Berserk[/eluser]
-change your output_buffering = ON
-check your file encode (i encoded in UTF-8 and get the same problem)


Cannot modify header information - El Forum - 11-21-2008

[eluser]Armchair Samurai[/eluser]
Berserk has an excellent point - if you've encoded in UTF-8, there might be the invisible BOM mark at the beginning of your files which needs to be removed (easy enough, depending on your text editor) otherwise you will get the "Cannot modify header information" error.

Also, unrelated to the problem at hand, you've failed to load the model admin_model in your controller function verify where you actually need to access it, as opposed to index where it's loaded but not used.


Cannot modify header information - El Forum - 11-21-2008

[eluser]Nathan Payne[/eluser]
How do I change output_buffering = ON?
What text editor is best for too see the BOM marks?

Thanks.


Cannot modify header information - El Forum - 11-21-2008

[eluser]Berserk[/eluser]
sorry i forgot, let's find in php.ini, also you must have permission to edit. I'm using Notepad++ with "Encode in UTF-8 without BOM" Mode. If you're encoding your controller file in UTF-8, please convert to ANSI or UTF-8 without BOM ( at Notepad++'s Format Option).