php 如何在 codeigniter 3 数据库中设置会话?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30086348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How I can set a session in codeigniter 3 database?
提问by Martin Rafael Pérez Lara
I'm using the new version(3.0.0). of CodeIgniter and I have a new problem, my sessions doesn't work. 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.
我正在使用新版本(3.0.0)。CodeIgniter 和我有一个新问题,我的会话不起作用。我的意思是,控制器中的代码是正确的,因为没有错误,但是,当我尝试在视图中打印 PHP 变量时,什么也没有。
I checked my table in the MySQL Server, and nothing, I don't now what is the problem. I put my code of config.php. (I don't understand a lot of things in this new version)
我在 MySQL Server 中检查了我的表,什么都没有,我现在不知道是什么问题。我把我的config.php代码。(这个新版本很多东西看不懂)
$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.
我必须将第一行添加到“使”会话有效,我不知道该配置是否正确以在数据库中创建会话。
If somebody has the same problem, help me please :( . My Session class has not been edited.
如果有人遇到同样的问题,请帮助我:(。我的 Session 类尚未编辑。
回答by Shaiful Islam
First of all CI3 session tableand 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 variableswith new configurationbut 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.
会话库可用的新特性(函数)很少。
RememberDon't forget it to load via autoload.php or loading $this->load->library('session');
before use it.
记住不要忘记通过 autoload.php 加载或$this->load->library('session');
在使用之前加载它。
回答by Jeremy Harris
If you added the "first line" (i.e. sess_table_name
) to make it work, that is because your sess_driver
value is set to database. Take a look at the list of supported driversand you will see that for file based sessions, it will default to that. In other words, if you remove these lines, it should work:
如果您添加了“第一行”(即sess_table_name
)以使其工作,那是因为您的sess_driver
值设置为database。查看支持的驱动程序列表,您将看到对于基于文件的会话,它将默认为该列表。换句话说,如果您删除这些行,它应该可以工作:
$config['sess_table_name'] = 'ci_sessions';
$config['sess_driver'] = 'database';
回答by Tpojka
Remove that added line and set:
删除添加的行并设置:
$config['sess_save_path'] = 'ci_sessions';
Rest of code should be ok. Link to docs.
其余代码应该没问题。 链接到文档。
回答by Raymond
Try this :
尝试这个 :
$config['sess_save_path'] = sys_get_temp_dir();