Session 如何用php编写或读取会话
我试图将一些数据写入会话并从中读取,但没有成功。以下是我尝试的代码:Session 如何用php编写或读取会话,session,cakephp,Session,Cakephp,我试图将一些数据写入会话并从中读取,但没有成功。以下是我尝试的代码: $currentid = $itemDetails['Item']['id']; $currenttemid11 = $this->Session>write('itemy',$currentid); print_r($this->Session->read('itemy')); 但是它没有显示任何内容。这是CakePHP 2.X还是3上的问题?我还没有使用CakePHP3,但是在2.X版本上,
$currentid = $itemDetails['Item']['id'];
$currenttemid11 = $this->Session>write('itemy',$currentid);
print_r($this->Session->read('itemy'));
但是它没有显示任何内容。这是CakePHP 2.X还是3上的问题?我还没有使用CakePHP3,但是在2.X版本上,
$this->Session
只能在控制器中使用。我更喜欢
CakeSession :: write('variable_name', 'value');
CakeSession :: read('variable_name');
在CakePHP 3中,您可以这样做:
$session = $this->request->session();
$session->read('name');
$session->write('name', 'value');
对于CakePHP 2:
$this->Session->read('name');
$this->Session->write('name', 'value');
您还可以在烹饪书中找到更多信息
$this->Session>write
是打字错误还是您用这种方式编写代码(在write
之前缺少->
)?您使用的是哪个CakePHP版本?您应该始终提及您使用的确切CakePHP版本。它仅在控制器中可用的原因是,您不应该从模型或视图向会话写入,因为这不是这些类的工作。