Welcome Guest, Not a member yet? Register   Sign In
Sessions not being set when setting from library
#1

[eluser]Unknown[/eluser]
I am about to pull my hair out over this, so someone please help!

The simplest case of this is as follows:

Controller:

Code:
<?php

class Test_Controller extends CI_Controller
{
    public function test_a()
    {
        $this->load->library('test_lib');
        $this->test_lib->test();
    }

    public function test_b()
    {
        var_dump($this->session->userdata('test'));
    }
}

Library:

Code:
<?php

class Test_lib
{
    protected $_ci;

    public function __construct()
    {
        $this->_ci=get_instance();
    }

    public function test_a()
    {
        $this->_ci->session->set_userdata('test',array('testdata'));
    }
}

Send a request to test_controller/test_a, then to test_b. You'll see that the data that is dumped is false, instead of the expected test data.
#2

[eluser]Sanjay Sarvaiya[/eluser]
Hi,
You are calling test() method on library which one is not exist I think it should be test_a();

Code:
$this->test_lib->test_a();
instead of  $this->test_lib->test();
for session you must have to load session library on construct of test_lib
Code:
$this->_ci->load->library('session');
#3

[eluser]PhilTem[/eluser]
Code:
public function __construct()
    {
        $this->_ci =& get_instance(); // Assign by reference, don't make a copy of the object
    }

That's probably your problem Wink




Theme © iAndrew 2016 - Forum software by © MyBB