使用数据库驱动程序时,Codeigniter会话未存储在数据库中

时间:2018-12-07 06:07:04

标签: codeigniter codeigniter-3

在实时服务器上使用数据库驱动程序时,Codeigniter会话未存储在数据库中。但这在当地环境良好。

Codeigniter版本:3.1.9 PHP版本:7.2.11

在我的codeigniter配置下面,

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

任何人都有主意,请告知。

1 个答案:

答案 0 :(得分:0)

首先,您需要更改配置以使用数据库,如下所示:

$config['sess_driver'] = 'database';
$config['sess_cookie_name'] = 'ci_sessions';
$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;

然后,您必须为您的会话创建一个表,并且我为此创建了一个迁移文件:

class Migration_Create_ci_sessions extends CI_Migration
{    
    private $table;

    public function __construct()
    {
        parent::__construct();
        $this->load->dbforge();
        $this->table = 'ci_sessions';
    }

    public function up()
    {
        $this->dbforge->drop_table($this->table, TRUE);

        $ci_sessions = '`id` VARCHAR(128) 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`)';

        $this->dbforge->add_field($ci_sessions);
        unset($ci_sessions);

        $this->dbforge->create_table($this->table, TRUE);
    }

    public function down()
    {
        $this->dbforge->drop_table($this->table, TRUE);
    }
}

或者,如果您愿意,也可以直接使用sql:

CREATE TABLE IF NOT EXISTS `ci_sessions` (
    `id` varchar(128) 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('database', 'session');

并正确设置数据库的配置:

'username' => 'username',
'password' => 'password',
'database' => 'database',