【问题标题】:How to edit a session after it's created创建会话后如何编辑会话
【发布时间】:2016-01-21 12:55:01
【问题描述】:

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

但我不知道我该怎么做。

【问题讨论】:

  • 这不起作用:PHP 致命错误:在非对象上调用成员函数 set()
  • $auth = $this->session->get('auth'); $auth['account_id']= '10';$this->session->set('auth',$auth);
  • 完美!非常感谢:)

标签: php session phalcon


【解决方案1】:

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

$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

【讨论】:

  • 欢迎@John.Thanks
【解决方案2】:

这应该可行:

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:
      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
          ));
      

      【讨论】:

        【解决方案5】:

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

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

        【讨论】:

          猜你喜欢
          • 2015-04-26
          • 2016-04-01
          • 2020-08-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-23
          相关资源
          最近更新 更多