Take the 2-minute tour ×

I can't understand the strange behaviour of sessions in CakePHP controller:

class UsersController extends AppController
    public $helpers = array('Session');
    public $components = array('Session'); 

    public function Do() {
        $this->Session->write('Foo', 'Bar');
        $_SESSION['Foo2'] = 'Bar2';

        echo $this->Session->read('Foo');
        echo $_SESSION['Foo2'];

First echo outputs nothing and the second one - Bar2. Why does native $_SESSION works but through CakePHP interface it's not available?

share|improve this question
it should work no problem in code , could u check again –  vikas tyagi Mar 14 '12 at 19:20
Do you have sessions properly set up on app/config/core.php? –  bfavaretto Mar 14 '12 at 21:25
@bfavaretto Yeah, it's set to php. –  Ockonal Mar 15 '12 at 7:10
add comment

1 Answer

up vote 1 down vote accepted

(This answer assumes CakePHP 1.3)

You said in the comments that you have Session.save set to php on your config file. In the same file, you should have Session.start set to true. If you don't, you have two options:

  • Actually setting Session.start to true on the config file, or
  • Calling $this->Session->activate() from the controller before using the Session component.


http://book.cakephp.org/1.3/en/view/1465/Session (oddly, Session.start and activate() are only documented for the session helper, but they apply to the component too). http://book.cakephp.org/1.3/view/931/CakePHP-Core-Configuration-Variables

share|improve this answer
add comment

Your Answer


