如何在创建会话后编辑会话

时间:2016-01-21 12:55:01

标签: php session phalcon

我正在使用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
    )); }

但我不知道我怎么能指出这一点。

5 个答案:

答案 0 :(得分:3)

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

$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

答案 1 :(得分:1)

这应该有效:

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

答案 2 :(得分:1)

我认为你可以这样使用: -

$auth = $this->session->get('auth'); 
$auth['account_id']= 10;
$this->session->set('auth',$auth);

答案 3 :(得分:0)

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

答案 4 :(得分:0)

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

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