Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter session data lost after redirect

I am using codeigniter 2.1.0.

I am trying to do a register/login function using the session library in the codeigniter.

The register/login with the session library worked fine for localhost, but when I put it live and tried it, the session does not work.

My controller login works this way. I check the credentials, once ok I set my session data and redirect to another page.

$user_data = array(
                   'username'       => $result->user_name,
                   'email'          => $result->user_email,
                   'userid'         => $result->user_id,
                   'role'           => $result->user_role,
                   'login_state'    => TRUE,
                   'lastlogin'      => time(),
               );

$this->session->set_userdata($user_data);           
print_r( $this->session->all_userdata());            
redirect(base_url('dashboard'));

at this point here when I print all my session data, they do print out. But at the dashboard controller side, when i attempt to print the session data out, they were not there anymore.

Any idea why? Thanks in advance for the help.

like image 694
kkh Avatar asked Jan 23 '13 05:01

kkh


2 Answers

if you are working in CI 3.x and just upgraded your server php version to php 7.x

Go to system/libraries/Session/session.php at Line no 281 and replace ini_set('session.name', $params['cookie_name']); by ini_set('session.id', $params['cookie_name']);

like image 150
user2818832 Avatar answered Oct 08 '22 13:10

user2818832


I'm not sure what exactly is the problem. Recently I faced this too..

It was working before in my development running php7.0.

Currently it is only working in my production server running nginx and php 5.6. My development server seems to be not working and keeps on regenerate new row in sessions table. My development server is using php7.1, on homestead virtualbox development environment, usually being used for Laravel projects.

I managed to get over this by taking this step.

1) Go to system/libraries/Session/Session.php

2) Comment session_start() by adding //. We want to relocate the sessionn_start().

3) Go down to line 315 where it says Security is king, and comment out until line 351

enter image description here

4) Then go to your main index.php ( the root index.php )

5) Add session_start() at the top once.

enter image description here

6) Okay try again. Hopefully it works. My guess is that it is not working with php 7.1 and some update need to be done in this Session.php file.

  1. My CI Version is 3.1.1

enter image description here

like image 28
Apit John Ismail Avatar answered Oct 08 '22 14:10

Apit John Ismail