问题
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', 'jhon@gmail.com');
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] => jhon@gmail.com
)
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
- Is it common behaviour of ci session?
- So, How to use set and get custom field with CI table session?
- Why the created custom field always lost after I move to another page?
- When I run this->session->sess_destroy(), why it is not remove any session in table session?
Thx in advance..
回答1:
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;
回答2:
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'
来源:https://stackoverflow.com/questions/20717777/how-to-use-session-table-in-code-igniter