• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
session data lost after redirect in CI 3

#1
hi ,
i using CI 3 ,
When I login, in login page i set user info in session by set_userdata ,
after set session redirect page To other controller but the session data which i set , was be destroyed

this is my session config

$config['sess_driver'] = 'database';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;

and also my session library is auto load ,
and i created ci_session table
by this code

CREATE TABLE IF NOT EXISTS `ci_sessions` (
`id` varchar(40) NOT NULL,
`ip_address` varchar(45) NOT NULL,
`timestamp` int(10) unsigned DEFAULT 0 NOT NULL,
`data` blob NOT NULL,
KEY `ci_sessions_timestamp` (`timestamp`)
);

when i set session insert row in this table but when I print_r ($_SESSION )
i see just [ __ci_last_regenerate ] => 1453970104
Reply

#2
You have to load that session on every page 

PHP Code:
   $session_data $this->session->userdata('logged_in');
 
   $data['username'] = $session_data['username']; 

userdata('what ever you have named your session')
Reply

#3
Not really related to your problem, but if you're using CI3, why don't you simply store info the PHP mode: $_SESSION['whatever'] = 'whatever you want stored';

The set_userdata was inserted in CI3 only to avoid breaking code of CI2.x.

Regarding what kaai3 said, you don't need to load session "on every page", as you already "autoloaded" it.

Could you show us how and where you store the session data with some code?
Reply

#4
(02-16-2016, 01:55 AM)Avenirer Wrote: Not really related to your problem, but if you're using CI3, why don't you simply store info the PHP mode: $_SESSION['whatever'] = 'whatever you want stored';

The set_userdata was inserted in CI3 only to avoid breaking code of CI2.x.

Regarding what kaai3 said, you don't need to load session "on every page", as you already "autoloaded" it.

Could you show us how and where you store the session data with some code?

//User Controller - user.php

/**
* User::login()
*
* Login the user by username and password
*
* @param string username
* @param string password
*
* @return boolean
*/
function login()
{
if(@$this->session->userdata('logged_in') == FALSE)
{
if($this->input->post('submit'))
{
$config_rules = array(
array(
'field' => 'username',
'label' => $this->lang->line('con_username'),
'rules' => 'required' ),
array(
'field' => 'password',
'label' => $this->lang->line('con_password'),
'rules' => 'required'),
);

$this->CI->form_validation->set_rules($config_rules);
$this->CI->form_validation->set_message('required', $this->CI->lang->line('con_the').' '.'{field}'.' '.$this->CI->lang->line('not_field_required'));
$this->CI->form_validation->set_error_delimiters('<span class="terr">', '</span>');
if($this->CI->form_validation->run())
{
$set_session_data = array();
$set_session_data['lang'] = $user_data->language_user;
$set_session_data['language_user'] = $user_data->language_user;
$set_session_data['customer_id'] = $user_data->customer_id;
$set_session_data['username'] = $user_data->username;
$set_session_data['email'] = $user_data->email;
$set_session_data['user_type'] = $user_data->user_type;
$set_session_data['user_level'] = @$user_data->user_type == "Admin" ? "1" : ($user_data->user_type == "User" ? "3" : "");
$set_session_data['logged_in'] = TRUE;
$set_session_data['start_time'] = time();

$this->session->set_userdata($set_session_data);

redirect('home');
}
}
}
$this->user_inner_template_view('user/login');
}
// --------------------------------------------------------------------

//Home Controller -- home.php

class Home extends MY_Controller
{
/**
* __construct()
*
* Constructor
*/
function __construct()
{
parent::__construct();
$this->CI =& get_instance();
$this->load->library('session');
echo "<pre>";
print_r($this->session->all_userdata());
echo "</pre>";
exit;


}
}

Output:
=======

Array
(
[__ci_last_regenerate] => 1455615513
[lang_constant] =>
)

MY Session Config
=================

$config['sess_driver'] = 'database';
$config['sess_cookie_name'] = 'cisession';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;
// --------------------------------------------------------------------
Reply

#5
Wow... there are so many "no-no"'s in there that I am afraid to look at the code. Anyways... I see that you define an array named $set_session_data... but you are using an object named $user_data. Where did you define that?... Now, if you allow me, I would like to throw up...
Reply

#6
I was facing the same issue and after a long research I found the solution.

My session is stored in db not in files but even database is also restricted to store the size, I think..

Its not any core issue of codeigniter or something that is session destroyed automatically or something else..

It is actually the load of data amount in session, if we save the more & more data inside session with Codeigniter("I am not sure about native session but for codeigniter I can confirm this"), it will destroy the session automatically.

So what you have to do is, to go to that code that you are writing to get the data from db or somewhere else and then saving into session in userdata, try to reduce the load of data saving inside the session and exclude that data which is not needed on page from session every time.

Let me know if still there is an issue..
Reply

#7
Sessions have a limit of about 4KB
Reply

#8
Your Database Session Table is wrong, your using the CodeIgniter 2.x Session Table.

This is the CodeIgniter 3.x Session Table:

PHP Code:
CREATE TABLE IF NOT EXISTS `ci_sessions` (
 
       `idvarchar(128NOT NULL,
 
       `ip_addressvarchar(45NOT NULL,
 
       `timestampint(10unsigned DEFAULT 0 NOT NULL,
 
       `datablob NOT NULL,
 
       KEY `ci_sessions_timestamp` (`timestamp`)
); 

See your id field size...
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#9
(12-22-2017, 04:45 PM)ItsJustATypo Wrote: Sessions have a limit of about 4KB

No, cookies have that limit not sessions. In CI < v3 cookies were used to store session user data. Therefor, sessions did (did because CI v2 is obsolete) have a 4K limit. Since CI v3+ that is not the case. The limit is technically the max memory that PHP allows. In practice that would be unwise.
Reply

#10
(03-28-2017, 03:58 AM)thakralrahul1310. Wrote: It is actually the load of data amount in session, if we save the more & more data inside session with Codeigniter("I am not sure about native session but for codeigniter I can confirm this"), it will destroy the session automatically.

Please show or describe in detail how you can confirm this. Thanks.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.