简体   繁体   English

如何在CodeIgniter会话中存储数组?

[英]How to store array in CodeIgniter sessions?

I am calling a controller with method setActiveId and store 1 array in session and display after storing by using var_dump($this->session->all_userdata()) it shows perfectly but in same controller when I print the session in other method say checkinkingPermission then the array in session is empty. 我正在使用setActiveId方法调用控制器,并在会话中存储1个数组,并在使用var_dump($this->session->all_userdata())存储后显示,它显示得很好,但是当我在其他方法中打印会话时在同一控制器中显示checkinkingPermission那么会话中的数组为空。

Now I repeat all steps with one additional step. 现在,我将所有步骤重复一个附加步骤。

I did store array in session like before and after storing I store one dummy variable in session like this, 我确实像在存储之前和之后一样在会话中存储数组,像这样在会话中存储一个虚拟变量,

$this->session->set_userdata('dummy','tesing');

and again print using var_dump($this->session->all_userdata()) it display all session with array and last dummy variable and when check in checkinkingPermission then array in session and dummy variable is shown perfectly (it solves my problem but its rough solution). 并再次使用var_dump($this->session->all_userdata())打印,它将显示所有具有数组和最后一个虚拟变量的会话,并且当签入checkinkingPermission时,会话和虚拟变量中的数组会完美显示(它解决了我的问题,但它的粗糙性解)。

I want to know did anything miss or what problem is there that array not save in first scenario (also array is not greater then 4Kb (CI Session limit)). 我想知道是否有任何遗漏或在第一种情况下无法保存数组的问题(数组不大于4Kb(CI会话限制))。

Update 更新资料

Method from model named connections_model 来自名为connections_model的模型的方法

public function setActiveFriend($id) {
    $this->load->model('friend_model');
    $this->session->set_userdata('AF',$id);
    if($id==$this->session->userdata('userid'))
    {
        $this->session->set_userdata('MEOWN',1);
    }
    else
    {
        $this->session->set_userdata('MEOWN',0);
    }
    $this->friend_model->setFP($id); // Calls Here a method that is defined in another model
}

Another method that defined in friend_model 在friend_model中定义的另一种方法

public function setFriendPermissions($id) {

    $this->session->set_userdata('CFP',array());
    $this->db->where('user_id',$id);
    $locs = $this->db->get('friend_p')->result_array();

    if(is_array($locs))
    {
        foreach($locs as $loc)
        {
            array_push($this->session->userdata['CP'],$loc['perm_id']);
        }
    }
    $this->session->set_userdata('dummy','tesing'); // Important Line
}

In above method I set values in session array if I want to see session values in same method then its set and viewed but the array is empty if I print session in any method of connections_model file. 在上面的方法中,如果要在同一方法中查看会话值,则在会话数组中设置值,然后设置并查看它,但是如果我在connections_model文件的任何方法中打印会话,则该数组为空。

Important If I wrote this line $this->session->set_userdata('dummy','tesing'); 重要说明:如果我在这行中写了$this->session->set_userdata('dummy','tesing'); then session array save and viewd in all methods and if I don't write that line session array is empty. 然后会话数组保存并在所有方法中查看,如果我不写,那行会话数组为空。

You're not accessing the session data properly: 您没有正确访问会话数据:

$this->session->userdata['CURRENTFPERMISSIONS']

That's what you have. 那就是你所拥有的。 Keep in mind that $this->session->userdata is a function, not an array, that said, try this 请记住,$ this-> session-> userdata是一个函数,而不是一个数组,也就是说,尝试一下

array_push($this->session->userdata('CURRENTFPERMISSIONS'),$locpermission['perm_id']);

Please see the documentation on session management 请参阅有关会话管理文档

I don't recommend using all cap keys for your arrays, as that convention is supposed to be used for constants. 我不建议对数组使用所有大写键,因为该约定应该用于常量。

In addition to the above, try changing this: 除上述内容外,请尝试更改此内容:

if(is_array($locpermissions))
{
        foreach($locpermissions as $locpermission)
        {
            array_push($this->session->userdata['CURRENTFPERMISSIONS'],$locpermission['perm_id']);
        }
}

to this 对此

if(is_array($locpermissions))
{
        $tmpArr = $this->session->userdata('CURRENTPERMISSIONS');
        foreach($locpermissions as $locpermission)
        {
            array_push($tmpArr,$locpermission['perm_id']);
        }
        $this->session->userdata('CURRENTPERMISSIONS',$tmpArr);
}

Although, the more I look at your code, the more I don't understand why you are going ahead and setting up a blank array in the session structure if it only ever gets populated inside a conditional. 虽然,我越看待您的代码,却越不理解为什么要继续进行并在会话结构中设置空白数组(如果仅在条件条件中填充它的话)。 May want to re-factor that a bit 可能想重构一下

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM