Welcome Guest, Not a member yet? Register   Sign In
Session days...
#1

[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()

#2

[eluser]TWP Marketing[/eluser]
It may not be a sessions problem. This comes up frequently, (search the forums??)
Check that you have REMOVED all php taqs at the bottom of you files (controllers, models, views)
This causes trouble when there is a blank space following that tag (it is difficult to locate)
Just don't use the closing php tab at the bottom of your pages, any of them.
#3

[eluser]mrh[/eluser]
[quote author="TWP Marketing" date="1343427252"]It may not be a sessions problem. This comes up frequently, (search the forums??)
Check that you have REMOVED all php taqs at the bottom of you files (controllers, models, views)
This causes trouble when there is a blank space following that tag (it is difficult to locate)
Just don't use the closing php tab at the bottom of your pages, any of them.[/quote]

Thanks... I will check that out.

Funny thing... Before I posted I did search the forums and got nothing. I just searched again and got a ton of posts... wonder why my first search didn't work? If I'd send the posts I just found I would not have posted... strange...
#4

[eluser]mrh[/eluser]
Well ok...

I've double checked and removed all blank space at the end of every php file. Still have the issue.

One of the other posts had an error with an index in an array being wrong. This was causing an exception to be thrown and causing output to occur. Not sure how I'm going to find something like that but it makes me wonder.
#5

[eluser]TWP Marketing[/eluser]
Not just the blank spaces, remove any closing php tag (if at the end of the page) also. It is not needed and can cause problems.

This isn't the only possible cause for the "header already sent problem" but it is common.
Check through the forum search results for other causes.
#6

[eluser]mrh[/eluser]
Sorry... should have been more clear. I have removed everything until the trailing } of the class. I will keep searching!
#7

[eluser]mrh[/eluser]
Found it. My form_validation.php had some junk at the bottom... Thanks again!




Theme © iAndrew 2016 - Forum software by © MyBB