Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Session 如何用php编写或读取会话_Session_Cakephp - Fatal编程技术网

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版本。它仅在控制器中可用的原因是,您不应该从模型或视图向会话写入,因为这不是这些类的工作。