CodeIgniter Forums
Cannot modify header information headers already sent - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: Cannot modify header information headers already sent (/thread-33161.html)



Cannot modify header information headers already sent - El Forum - 08-17-2010

[eluser]Deathmanlp[/eluser]
start.php

Code:
<?php
class Start extends Controller
{
  function Start ()
  {
    parent::Controller ();
    $this->load->library('session');
    $this->load->helper('form');
    $this->load->helper('url');
    $this->load->library('form_validation');
    $this->load->model("sites_model");
  }

   function _check_login()
  {
    $username=$this->input->post("username");
    $password=$this->input->post("password");
    if ($username=="ge0rgi" and $password=="123456" )
    {
      $this->session->set_userdata("status", "logged_in");
      $this->load->view("add_site_view");
    }

    else
    {
      echo "Username and/or password wrong!";
      $this->load->view("login");
    }
  }

  function index ()
  {
    if (isset ($this->session->userdata) and $this->session->userdata("status")=="logged_in" )
    {
      $this->load->view('add_site_view');
    }
    else
    {
      $this->load->view("login");
    }
  }
  
  function validate_login_form ()
  {
    $this->form_validation->set_rules ("username","Username","required");
    $this->form_validation->set_rules ("password","Password","required");
    if ($this->form_validation->run()==TRUE)
    {
      $this->_check_login ();    
    }
    else
    {
    $this->load->view("login");
    }
  }
  
  function validate_add_site_form ()
  {
  $url_regex='|[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i';
  $url=$this->input->post("url");
  $this->form_validation->set_rules("name", "Site name", "required|alpha_dash|max_length[100]");
  $this->form_validation->set_rules("url", "Site URL", "required|prep_url|max_length[100]");
  if ($this->form_validation->run() and preg_match ($url_regex,$url )==TRUE)
    {
    $this->sites_model->_add_site();
    $data= array ();
    $data["changes"]=$this->db->affected_rows();
    $this->load->view("add_success", $data);
    
    }
    else
    {
    echo $this->load->view("add_site_view");
    }
  }
}
?>

login.php

Code:
<?php echo validation_errors () ; ?>
<?php echo form_open('start/validate_login_form'); ?>
<h1>Please Log in <br/></h1>
Username: &nbsp;
&lt;?php echo form_input("username", set_value("username")); ?&gt;
<br/> Password: &nbsp;
&lt;?php echo form_password("password"); ?&gt;
<br/>
&lt;?php echo form_submit("value","Log in"); ?&gt;
&lt;?php echo form_close (); ?&gt;

site_model.php

Code:
&lt;?php
Class Sites_model extends Model
{
  function _add_site ()
  {
    $data=array ();
    $data["name"]=$this->input->post("name");
    $data["url"]=$this->input->post("url");
    $this->db->insert("sites",$data);
  }
}
?&gt; // line 12

The error

Quote:Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at J:\xampp\htdocs\ci\application\models\sites_model.php:12)

Filename: libraries/Session.php

Line Number: 662

This happens after a successful login. I get this error and my add_site_view under it.
I checked all files for empty spaces before &lt;?php and after ?&gt; All files are encoded UTF-8 UNIX without BOM.

I had a lot of errors like this and I fixed them, but I can't get rid of this one.


Cannot modify header information headers already sent - El Forum - 08-17-2010

[eluser]danmontgomery[/eluser]
You have whitespace after the closing PHP tag in sites_model.php. You can remove it, or just remove the closing php tag.


Cannot modify header information headers already sent - El Forum - 08-17-2010

[eluser]Deathmanlp[/eluser]
Thanks a lot noctrum I checked the file several times I guess the editor is adding the space after ?&gt; After removing it everything works fine Smile