简体   繁体   English

如何在codeigniter 3数据库中设置会话?

[英]How I can set a session in codeigniter 3 database?

I'm using the new version(3.0.0). 我正在使用新版本(3.0.0)。 of CodeIgniter and I have a new problem, my sessions doesn't work. CodeIgniter和我有一个新问题,我的会话不起作用。 I mean, the code in the controller is correct because there are not errors but, when I try to print a PHP variable in a view there is nothing. 我的意思是,控制器中的代码是正确的,因为没有错误但是,当我尝试在视图中打印PHP变量时,什么都没有。

I checked my table in the MySQL Server, and nothing, I don't now what is the problem. 我在MySQL服务器上检查了我的表,什么都没有,我现在不知道是什么问题。 I put my code of config.php. 我把我的config.php代码。 (I don't understand a lot of things in this new version) (我对这个新版本中的很多东西都不了解)

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

I have to add the first line to "make" sessions works, I don't know if that configuration is correct to make sessions in a database. 我必须添加第一行到“make”会话工作,我不知道该配置是否正确在数据库中进行会话。

If somebody has the same problem, help me please :( . My Session class has not been edited. 如果有人有同样的问题,请帮助我:(。我的Session类尚未编辑。

First of all CI3 session table and CI2 session table( Saving Session Data to a Database )structure is different 首先, CI3会话表和CI2会话表( 将会话数据保存到数据库 )结构不同

New session table structure 新的会话表结构

 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,
    PRIMARY KEY (id),
    KEY `ci_sessions_timestamp` (`timestamp`)
);

Second They support old configuration variables with new configuration but it is better to use new configuration 其次它们使用新配置支持旧配置变量 ,但最好使用新配置

$config['sess_driver'] = 'database';
$config['sess_cookie_name'] = 'ci_sessions';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = 'ci_sessions';//its your table name name
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;

See more details at their docs 在他们的文档中查看更多详细信息

Few new feature(function) available for session library. 会话库几乎没有新功能(功能)可用。

Remember Don't forget it to load via autoload.php or loading $this->load->library('session'); 记住不要忘记通过autoload.php加载或加载$this->load->library('session'); before use it. 在使用之前。

If you added the "first line" (ie sess_table_name ) to make it work, that is because your sess_driver value is set to database . 如果您添加了“第一行”(即sess_table_name )以使其正常工作,那是因为您的sess_driver值设置为database Take a look at the list of supported drivers and you will see that for file based sessions, it will default to that. 查看支持的驱动程序列表,您将看到对于基于文件的会话,它将默认为该。 In other words, if you remove these lines, it should work: 换句话说,如果你删除这些行,它应该工作:

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

Remove that added line and set: 删除添加的行并设置:

$config['sess_save_path'] = 'ci_sessions';

Rest of code should be ok. 其余的代码应该没问题。 Link to docs . 链接到文档

尝试这个 :

$config['sess_save_path'] = sys_get_temp_dir();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM