[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);
}
}
}
?>
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><html>
<head><TITLE>Main</TITLE></head>
<body>
<h1>Welcome</h1>
</body>
</html>
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!