Welcome Guest, Not a member yet? Register   Sign In
Session Bug? When setting CI custom session info I get "Message: Cannot modify header information ..."
#1

[eluser]Unknown[/eluser]
Hi everyone,

I am pretty new to CI, been working with PHP for a while though.
I am moving a website from smarty to CI. I am trying to use CI sessions and I am getting a weird error, let's see if I can explain it clearly.

I have created a parent Controller class for all those Controllers which can have methods that require login.
My parent class looks like this:
Code:
<?php
abstract class LogController extends Controller {

  function LogController() {
    parent::Controller();
  }
  abstract function needsLogin ($method);

  function _remap($method)
  {
    if ($this->needsLogin($method))
    {
      $this->session->set_userdata('test','1');
      echo 'test';
    }
    else { $this->$method();}
  }
}
?>

And I have some controller that looks like this:
Code:
<?php
include_once('logcontroller.php');
class TestController extends LogController {

  private $loginMethods = array("method1");
  
  function TestController() {
    parent::Controller
  }
  
  function needsLogin ($method) {
    if (in_array($method,$this->loginMethods)) return true;
    return false;
  }

  function method1() {
    // do whatever
  }    
?>

The problem I have is that when I try to set the session variable on the parent class (inside the _remap method) I am allways getting an error like this:

Message: Cannot modify header information - headers already sent by (output started at C:\CIPATH\system\application\controllers\logcontroller.php:18)
Filename: libraries/Session.php
Line Number: 295



The line number of Session.php that apparently is generating the error refers to a setcookit function call.

If I do the same setting in the child class instead (i.e. setting the 'test' session variable inside the needsLogin method of TestController class) everything works fine.
Any idea why this is happenning?

I have look for any similar error all over the forum and did not find anything related.

All the settings (config, autoload) are ok, otherwise the setting for the session variable in the TestController class will not work.
I am thinking to switch to native PHP session handling but I'd like to give CI sessions a try.
I know that I could do what I want to achieve in some other way, but this is the one I think is cleaner and simpler.

Thank you very much in advance to everyone.

Regards from Barcelona

1/2 Desparpajo
#2

[eluser]Derek Allard[/eluser]
This sounds like it isn't a bug in sessions, but rather its because you've got content getting echoed out somewhere. Usually this is a return before <?php or possibly a space after the closing ?>. Search the forums for "Cannot modify header information" and you'll get tonnes of hits.

A sincere welcome to the CI community desparpajo! You're going to love it!
#3

[eluser]Unknown[/eluser]
You're right Derek!

Thanks a lot. I was going crazy, tried a lot of ways for doing the same.

For the record, the problem was that I had (as you pointed out) a space after the closing ?>.

Thank you again, I will go on using CI sessions, and trying to enjoy CI.

Best regards!


Desparpajo
#4

[eluser]Derek Allard[/eluser]
Glad it was helpful! I know you're just getting started with CI, but I'm going to bury you in new information ok Wink

We keep a subversion repository where we add new stuff that will make it into the next release of CI. In it, there's a new session library (and userguide page) that offers some pretty nice enhancements. I wrote about it in my blog if its of interest to you.




Theme © iAndrew 2016 - Forum software by © MyBB