Home » Php » php – Session ID is changing in CodeIgniter

php – Session ID is changing in CodeIgniter

Posted by: admin July 12, 2020 Leave a comment


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


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?

How to&Answers:

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));


By replacing the directory system/libraries/ session per the most recent version (from the CI website), the problem will be solved.
There were several bugs in the session libraries of CI and they are already resolved.



You could mention the link of the file will be useful for the beginners.

i.e. CodeIgniter/system/libraries/Session.php


Changing session_id is normal in CI.
But I had a problem. Session values were not saving, not persistent only in CI.

I fixed it by updating Codeigniter to the latest version, which is currently


One possible issue that you can have i described here: https://github.com/EllisLab/CodeIgniter/issues/3108

The thing is stupid as it can be. If you set cookie name with dots it will regenerate session id every time you reload the page. And of course, you would not be able to keep any user specific data!


If you don’t want to update session on every five minutes do the following changes.

go to the session.php file in the System/libraries/Session.php
and set all parameters like
public $sess_encrypt_cookie;public $sess_use_database;… to blanks
It’s working for me