简体   繁体   中英

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.

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

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

Update

Method from model named 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

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.

Important If I wrote this line $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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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