0
votes

I have a login function. When I login, the session gets saved. But when I refresh the page or redirect to another function, then the session (userdata) is shown blank. I have loaded the session library in autoload, but the userdata is deleted after every page refresh.

Here is my code.

public function index () {
$user = $this->input->post('user');
// after successful user checking
$this->session->set_userdata('user', $user);
// when I print session here,
print_r($this->session->all_userdata());
// session user gets print
}

But when I redirect to a function (suppose 'test'), then no any session is shown.

public function test() {
print_r($this->session->all_userdata());
die;
}
2
Note: Codeigniter by default uses Cookie-based mechanism so it's provides limited storage. If you want to extend the storage limit you can configure it to store session data in database. Check CI Documentation.Indrasinh Bihola
Ya but I'm not storing excessive data in session that my session gets deleted.Sajan Gurung
Ok than try to debug in both functions using print_r($this->session);Indrasinh Bihola

2 Answers

0
votes

When you read the post value with $this->input->post('user'); and there isn't any post the function returns null and save this in the user value.

You have to check before setting.

    if ($this->input->post('user')) {
        $this->session->set_userdata('username',  $this->input->post('user'));
    }
0
votes

I solved the problem. Actually, accidentally I had destroyed the session at the beginning of the code. So, my session was all destroyed. I removed the code and it's working fine.