[eluser]mrh[/eluser]
I notice there was another "session" related post so I guess today is session day!
I'm not using the session database table. I do have my key set. I've printed out the session info when loading a view and it looks ok:
Code:
[27-Jul-2012 14:48:39] Apply session data: Array
(
[session_id] => 0c82f23e450faf90017e5cc7513b4bf8
[ip_address] => 127.0.0.1
[user_agent] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11
[last_activity] => 1343415141
[user_data] =>
)
All good so far. I have a small admin user login that accepts a username and password. I have form validation enabled. There are three rules: The first two just trim and make required the username and password. The final rule is a callback called "check_login()". This callback grabs the username/password from the post data and looks up the user in the database. If the username doesn't exist or the password is wrong then it returns FALSE and sets and error message. All of this works.
When I type in a correct username and password and all the validation has passed my code in my submit function looks like below. The data model is loaded, validation runs, if it succeeds I load the admin user and get the ID. If ID>0 then I want to "log them in".
So I try to set a small piece of user session data: $this->session->set_userdata( 'adminloginID', $adminID );
just prior to loading the portal view. But I keep getting this message at the top of my portal view:
A PHP Error was encountered
Severity: Warning
Message: Cannot modify header information - headers already sent by (output started at /Users/mrh/Dropbox/Dev/MAMP/htdocs/entci/application/config/form_validation.php:241)
Filename: libraries/Session.php
Line Number: 672
This seems to happen whenever I try setting ANYTHING into the session user data. I thought this would be OK to do just prior to loading a view. Is there something I must do to kind of "reset" for a fresh view?
I am aware that when setting $_SESSION values in normal PHP you have to do them before sending ANYTHING to the browser. I am guessing by this error message the same is true of the session data in CI. But why is "form_validation" sending output?
What am I missing? I have read the session user guide numerous times. Thanks in advance!
Code:
public function submit()
{
// Load the database
$this->load->model( 'data_model' );
$this->data_model->loadDatabase();
//
// Apply validation rules are set in form_validation.php in the config folder
//
$this->load->library('form_validation');
if( $this->form_validation->run('admin')==FALSE )
{
//
// Validation failed. Show the login form again
//
self::index();
}
else
{
$name = $this->input->post('userName');
$pass = $this->input->post('userPass');
$adminID = $this->data_model->checkAdminlogin( $name, $pass );
if( $adminID>0 )
{
error_log( "set user data... loading portal view" );
$this->session->set_userdata( 'adminloginID', $adminID );
// Setup menus for this view - No highlight
$menuData['menus'] = self::constructMenu( "" );
// Load the view
$this->load->view( 'header', $menuData );
$this->load->view( 'admin_portalview' );
$this->load->view( 'footer', $menuData );
}
}
} // submit()