简体   繁体   中英

Native PHP Session values are lost in Codeigniter

I am working on a project that uses native PHP sessions . I redirect user to Codeigniter page and all the session values are lost. I tried using Nativesession library, it helps me retain all the native php session data and same is true if I use session_start() in index.php of Codeigniter but I get notice messages saying " A session is active, You cannot change the session module's ini settings at this time " and " A session had already been started - ignoring session_start() "

you have to start the session like this

add this to your controller constructor

$this->load->library('session')

you can use the session like this

$this->session

add data

$this->session->set_userdata('some_key', 'some_value');

fetch data

$some_value = $this->session->userdata('some_key');

Read the docs

Codeigniter Session Management

After reading about how the session library works in codeigniter, figured out the root cause of the problem.

As per documentation,

" When a page is loaded, the session class will check to see if valid session cookie is sent by the user's browser. If a sessions cookie does not exist (or if it doesn't match one stored on the server or has expired) a new session will be created and saved. "

In my case, when I use PHP Native sessions , it doesn't create valid cookies for CI and as per above statement, CI will create new session hence old session will be destroyed.

there are two ways to get around this

  1. suppress the Warnings and notices generated by CI session library by using @$this->load->library('session')
  2. Don't load the CI session library at all and keep using Native PHP sessions.

Hope it helps if anyone is facing same issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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