Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Session ID is changing in CodeIgniter

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?

like image 615
Doug Molineux Avatar asked Aug 08 '11 02:08

Doug Molineux


2 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));
like image 154
laurent Avatar answered Sep 30 '22 11:09

laurent


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.

like image 38
louk Avatar answered Sep 30 '22 12:09

louk