Ayip.Eiger
Ayip.Eiger

Reputation: 11

How to use session table in code igniter

I'm new on code igniter and wanna ask how to use session with session table in database. I have create session table with this sctructure.

CREATE TABLE `sys_sessions` (
    `session_id` VARCHAR(40) NOT NULL DEFAULT '0' COLLATE 'utf8_unicode_ci',
    `ip_address` VARCHAR(16) NOT NULL DEFAULT '0' COLLATE 'utf8_unicode_ci',
    `user_agent` VARCHAR(50) NOT NULL COLLATE 'utf8_unicode_ci',
    `last_activity` INT(10) UNSIGNED NOT NULL DEFAULT '0',
    `user_data` TEXT NOT NULL COLLATE 'utf8_unicode_ci',
    PRIMARY KEY (`session_id`)
)
COLLATE='utf8_unicode_ci'
ENGINE=InnoDB;

and use two controller to test session used. First I run set_session_test() controller.

public function set_session_test()
{
   $this->load->library('session');
   $this->session->set_userdata('username', 'Jhon');
   $this->session->set_userdata('email', '[email protected]');
   echo "<pre>"; print_r($this->session->all_userdata()); echo "</pre>";
}

And the result

Array
(
    [session_id] => dd7e0e2266da6481ef45faaa7e3c808b
    [ip_address] => 127.0.0.1
    [user_agent] => Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
    [last_activity] => 1387619782
    [user_data] => 
    [username] => Jhon
    [email] => [email protected]
)

then I run get_session_test() to check if it session value could be still there.

public function get_session_test()
{
   $this->load->library('session');
   $this->session->userdata('username');
   $this->session->userdata('email');
   echo "<pre>"; print_r($this->session->all_userdata()); echo "</pre>";
}

And the result is

Array
(
    [session_id] => 1b10e3671728804a63266bb9295b7e5d
    [ip_address] => 127.0.0.1
    [user_agent] => Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
    [last_activity] => 1387619831
    [user_data] => 
)

Unfortunately, I didn't found any username and email session that i stored. And i check table sys_session, it creates two row of session_id. The question is

  1. Is it common behaviour of ci session?
  2. So, How to use set and get custom field with CI table session?
  3. Why the created custom field always lost after I move to another page?
  4. When I run this->session->sess_destroy(), why it is not remove any session in table session?

Thx in advance..

Upvotes: 1

Views: 18874

Answers (3)

Jozeph OFATTOLE
Jozeph OFATTOLE

Reputation: 1

$this->session->set_userdata('username', 'Jhon'); $this->session->set_userdata('email', '[email protected]');

according to this lines :

if your session driver is configured to use database then your data (username,email) will be stored in the user_data field of the ci_sessions table

Upvotes: 0

saurabh kamble
saurabh kamble

Reputation: 1549

There is already provision made in codigntier to store session

CREATE TABLE IF NOT EXISTS  `ci_sessions` (
    session_id varchar(40) DEFAULT '0' NOT NULL,
    ip_address varchar(45) DEFAULT '0' NOT NULL,
    user_agent varchar(120) NOT NULL,
    last_activity int(10) unsigned DEFAULT 0 NOT NULL,
    user_data text NOT NULL,
    PRIMARY KEY (session_id),
    KEY `last_activity_idx` (`last_activity`)
);

Once you have created your database table you can enable the database option in your config.php file as follows:

$config['sess_use_database'] = TRUE;

Upvotes: 3

BMN
BMN

Reputation: 8508

This behaviour happens when your user_agent field in your database is too short :

`user_agent` VARCHAR(50) NOT NULL COLLATE 'utf8_unicode_ci'

Actually, when you count the characters in your current user_agent, you have more than 50 characters, so CI won't find any session in the database with this user_agent, as it was previously stored truncated to 50 characters.

Try to make this field bigger it should solve your problem :

`user_agent` VARCHAR(250) NOT NULL COLLATE 'utf8_unicode_ci'

Upvotes: 0

Related Questions