Welcome Guest, Not a member yet? Register   Sign In
puzzled
#1

[eluser]ngilbert[/eluser]
I've been fighting with this for a couple of hours now. I'm getting two instances of this php error: "Message: Cannot modify header information - headers already sent by" yadda yadda. When I view the page source, the first character is a space. I'll post the page source at the bottom.

Below is the code. The error is happening on the set_cookie methods, being called inside of my login function.

Code:
<?php
class Users extends Controller
{
  public function __construct()
  {
   parent::Controller();

   // Load any class dependencies.
   $this->load->helper(array('html', 'Form', 'cookie'));
   $this->load->library('validation');
  }

  public function index()
  {
   $rules['username'] = "required|min_length[5]|max_length[20]";
   $rules['password'] = "required";
   $fields['username'] = 'Username';

   $this->validation->set_rules($rules);
   $this->validation->set_fields($fields);
   $this->validation->set_error_delimiters('<div class="error">', '</div>');

   if($this->validation->run() == FALSE)
   {
    $this->load->view('login_view');
   }
   else
   {
    $this->login();
   }
  }


  /*-----------------------------------------------------------
   | Summary : Validates login credentials.  Once user
   |    is validated, query database for user.
   |    If user exists, set cookie.  Otherwise
   |    display login screen with error message.
    -----------------------------------------------------------*/
  public function login()
  {
   // Verify login credentials with the database.
   $sql = "SELECT id FROM accounts WHERE username = ? AND password = ?";
   $query = $this->db->query($sql, array($_POST['username'], md5($_POST['password'])));

   if($query->num_rows() == 1)
   {
    $row = $query->row();

    // If login is successful set cookie(s).    
    $user_cookie = array('name' => 'account_id', 'value' => $row->id, 'expire' => '0');
    $pass_cookie = array('name' => 'password', 'value' => md5($_POST['password']), 'expire' => '0');
    set_cookie($user_cookie);
    set_cookie($pass_cookie);

    $this->load->view('main_view');

   }
   else
   {
    $data['login_error'] = 'Login incorrect.';
    $this->load->view('login_view', $data);
   }
  }
}
?&gt;

Page Source:
Code:
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message:  Cannot modify header information - headers already sent by (output started at /home/ngilbert/public_html/www/contract/system/application/controllers/users.php:76)</p>
<p>Filename: helpers/cookie_helper.php</p>
<p>Line Number: 90</p>

</div><div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message:  Cannot modify header information - headers already sent by (output started at /home/ngilbert/public_html/www/contract/system/application/controllers/users.php:76)</p>
<p>Filename: helpers/cookie_helper.php</p>
<p>Line Number: 90</p>

</div>&lt;html&gt;
&lt;head&gt;&lt;TITLE&gt;Main&lt;/TITLE&gt;&lt;/head&gt;

&lt;body&gt;

<h1>Welcome</h1>

&lt;/body&gt;
&lt;/html&gt;

For the life of me, I can't see what is causing the header information to be sent prematurely. If you notice any problems with my code, please let me know.

Thanks!
#2

[eluser]Unknown[/eluser]
The main problem seems to be some withe space

before
Quote: &lt;?php tag or after ?&gt; tag
in your users.php file

remove the white space will solve the problem
#3

[eluser]linuxbz[/eluser]
One easy thing you can do is eliminate the final
Code:
?&gt;
tag.

PHP will add it automatically if it is missing, without any whitespace following it.
#4

[eluser]ngilbert[/eluser]
Wow! Thank you very much krishnaraj and linuxbz. That was exactly my problem. I was unaware that trailing whitespace after a closing php tag could cause any problems. Lesson learned.




Theme © iAndrew 2016 - Forum software by © MyBB