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?
(This answer assumes CakePHP 1.3)
You said in the comments that you have
Session.saveset tophpon your config file. In the same file, you should haveSession.startset totrue. If you don’t, you have two options:Session.starttotrueon the config file, or$this->Session->activate()from the controller before using the Session component.References:
http://book.cakephp.org/1.3/en/view/1465/Session (oddly,
Session.startandactivate()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