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

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
    )); }

但我不知道我怎么能点那个。

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
    ));

这应该有效:

$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);

您需要按以下方式进行:-

$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

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

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