如何在创建会话后对其进行编辑


How to edit a session after it's created

我正在使用Phalcon PHP,我想在创建会话后向会话添加另一个项目。我有这个:

private function _registerSession($user, $account) {
    $this->session->set('auth', array(
        'user_id' => $user->id,
        'username' => $user->name
    )); }

在另一个控制器中,我想编辑此会话,例如:

$auth = $this->session->get('auth');
$auth->add('account_id', '10');

本次会议将把 3 个变量的内容内容为:

    $this->session->set('auth', array(
        'user_id' => $user->id,
        'username' => $user->name,
        'account_id' => 10
    )); }

但我不知道我该怎么加点。

Yo 需要按以下方式执行此操作:-

$auth = $this->session->get('auth'); // get auth array from Session
$auth['account_id']= '10'; // add new index value pair to it
$this->session->set('auth',$auth); // reassign it to auth index of Session

这应该有效:

$auth = $this->session->get("auth");
$this->session->set("auth", array_merge($auth, array('account_id'=>'10')));
我认为

你可以这样使用:-

$auth = $this->session->get('auth'); 
$auth['account_id']= 10;
$this->session->set('auth',$auth);
private function _registerSession($user, $account) {
    $this->session->set_userdata('auth', array(
        'user_id' => $user->id,
        'username' => $user->name
    )); }
// You should use the following code to set one more parameter in sesssion:
   $this->session->set_userdata('auth', array(
        'user_id' => $this->session_userdata('user_id'),
        'username' => $this->session_userdata('username'),
        'account_id' => 10
    ));

Phalcon的会话代码只是$_SESSION的包装器。 最简单的解决方案是避免使用 Phalcon 函数:

$_SESSION['auth']->add('account_id',10);