Session ID is changing in CodeIgniter

Doug Molineux picture Doug Molineux · Aug 8, 2011 · Viewed 20k times · Source

I am using the session feature of CodeIgniter, and I'm running this code:

$session_id = $this->session->userdata('session_id');
echo "My Session ID is $session_id"; 

And it keeps changing every time I load the page. Does this mean that sessions are not being saved properly? Is there any way to debug this and find out why? Or am I not using this library correctly?

I don't get any errors when I enable error reporting, and I'm using the autoload ability of CI to load the session library:

$autoload['libraries'] = array('session');

Any advice would help thanks!

Example Output of the Code above:

My Session ID is 7c92bac53d2654df6e87eb4e4cb25467

.. reload ..

My Session ID is c6dd14aed2499760f788a1364dcab030

UPDATE: My session configurations inside config.php look like this:

$config['sess_cookie_name']     = 'ci_session';
$config['sess_expiration']      = 7200;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie']  = FALSE;
$config['sess_use_database']    = FALSE;
$config['sess_table_name']      = 'ci_sessions';
$config['sess_match_ip']        = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update']  = 300;

So I have found that

session_start();
$_SESSION['myvar']='var'; 

doesn't store anything either, something seems to be wrong with the session storage on my linux server.

My session save path has apache:apache 775 permissions. Perhaps this should be moved to serverfault?

Answer

laurent picture laurent · Aug 8, 2011

According to CI's Session.php, the ID is changed on every update, but they keep a reference to the old ID so that they can update it right row.

Also, according to the doc: "session_id" is regenerated (by default) every five minutes".

Is there any reason why you need to access "session_id"? If you need some sort of fixed ID, you should create your own "my_session_id", that way it won't change between request.

$uniqueId = uniqid($this->CI->input->ip_address(), TRUE);
$this->session->set_userdata("my_session_id", md5($uniqueId));