Codeigniter不会在数据库上保存会话

时间:2016-02-27 02:01:45

标签: mysql codeigniter session session-cookies

我试图在数据库上保存会话但没有成功。我已经设置了CI配置,自动加载,没有任何作用。

在我的控制器上

$session_data = array(
   'session_id' => session_id(),
   'ip_address'  => $this->input->ip_address(),
   'user_agent'     => $this->input->user_agent(),
   'last_activity' => substr($_SERVER['HTTP_USER_AGENT'], 0, 120),
   'user_data' => 'logged'
 );

$this->session->set_userdata('ci_sessions', $session_data);

自动加载

$autoload['libraries'] = array('database','session');
$autoload['drivers'] = array('session');

配置

$config['encryption_key'] = 'wqf35MV437ytPyGweGHH24775zK8u7uD';
$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_sessions';
$config['sess_encrypt_cookie'] = FALSE;
$config['sess_expiration'] = 8640;
$config['sess_use_database'] = TRUE;
$config['sess_table_name']  = 'ci_sessions';
$config['sess_expire_on_close'] = TRUE;
$config['sess_save_path'] = NULL;
$config['sess_match_ip'] = TRUE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;

MYSQL上的默认ci_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')
);

1 个答案:

答案 0 :(得分:0)

您已将CI2会话配置与CI3会话配置混合在一起。

会话Database User Guide CI3

如果您使用CodeIgniter 3如果您想将会话保存到数据库

更改

$config['sess_driver'] = 'files';
$config['sess_save_path'] = NULL;

$config['sess_driver'] = 'database';

// This will be your table name for session

$config['sess_save_path'] = 'ci_sessions';
$config['sess_expiration'] = 7200;
$config['sess_match_ip'] = TRUE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;

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`)
);

然后我会自动加载会话

$autoload['libraries'] = array('session');