CodeIgniter 3 and sessions

2020-07-06 04:09发布

I updated to CodeIgniter 3 recently, following this guide: CI3: upgrade 3.0 from 2.2.1.

I set up this configuration in application/config/config.php file:

$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session_my_site';
$config['sess_expiration'] = 604800; // 1 week
$config['sess_save_path'] = NULL;
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;

Is there something wrong here? My session is destroyed after a few hours...

4条回答
在下西门庆
2楼-- · 2020-07-06 04:35

In your save path you need to set up a location folder. Use 'files' as session driver preferred. As like below I have set up a cache to store sessions in BASE PATH which is setting folder. Make sure you have auto loaded sessions and $config['encryption_key'] = '' add key.

You can set up a database sessions but this works just as well. Make sure folder permissions 0700

http://www.codeigniter.com/userguide3/search.html?q=session&check_keywords=yes&area=default

$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 1440;
$config['sess_save_path'] = BASEPATH . 'yourfoldername/cache/';
$config['sess_match_ip'] = TRUE;
$config['sess_time_to_update'] = 300;

Once that is done then you should be able to do something like.

$this->load->library('session');
$data_session_set = array('logged' => 1, 'username' => $this->input->post('username'), 'user_id' => $this->user_auth->get_id()); 
$this->session->set_userdata($data_session_set);
查看更多
放我归山
3楼-- · 2020-07-06 04:49

I use this code it work.

    if (version_compare(PHP_VERSION, '5.4.0', '<')) {
        if(session_id() == '') session_start();
    } else  {
       if (session_status() == PHP_SESSION_NONE) session_start();
    }
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2020-07-06 04:52

I have used 2 different values and somehow they worked, it depends on the version of Centos:

For Centos 6.X I used:

In application/config/config.php set this value:

$config['sess_save_path'] = NULL;

For Centos 7.X I used:

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

it will not work if you move your app from one server version to another without editing this value.

查看更多
We Are One
5楼-- · 2020-07-06 04:57

In application/config/config.php set this value:

$config['sess_save_path'] = sys_get_temp_dir();
查看更多
登录 后发表回答