Setting Session_id in CodeIgniter

2019-05-04 13:09发布

问题:

In standard PHP i can set the session ID by following:

$my_session_id = $_GET['session_id']; //gets the session ID successfully
session_id($my_session_id);   //sets the session with my session_id
session_start();  //starts session.

How can I do same with the CodeIgniter? I am trying following:

$my_session_id = $_GET['session_id']; //gets the session ID successfully
$this->session->userdata('session_id', $my_session_id); //it won't set the session with my id.
print_r($this->session->userdata);

Am i making some mistake? Please help me as I have wasted several hours in this problem. If there is some issue with CodeIgniter Session class, can I use the standard PHP code to start session? I have tried to place the standard code in CodeIgniter as well, but it still does not set the session_id. I have also set $config['sess_match_useragent'] = FALSE;

回答1:

This is a hack I just suggested on another question. Place this in the __construct of MY_Session (untested and will not work with encryption)

public function __construct()
{
   if( isset( $_GET['session_id'] ) ) 
        $_COOKIE[ $this->sess_cookie_name ] = $_GET['session_id'];
   // Session now looks up $_COOKIE[ 'session_id' ] to get the session_id
   parent::__construct()
}


回答2:

You shouldn't need to do this, codeigniter does it all for you.

If you wish to get the session id you can do so by calling:

$session_id = $this->session->userdata('session_id');

However you can work around it: ( Note this post is 3 years old and I'm unsure if it's still necessary )

http://mumrah.net/2008/06/codeigniter-session-id/



回答3:

use this for setting session_id in codeigniter:

$this->session->set_userdata( array('session_id', $your_session_id) );

and

$session_id = $this->session->userdata('session_id');

for reading it out again.



回答4:

You should always start your session before modifying any session variables.

I believe to start the CodeIgniter session you can do the following;

$this->load->library('session');
$my_session_id = $_GET['session_id']; //gets the session ID successfully
$this->session->userdata('session_id', $my_session_id); //it won't set the session with my id.
print_r($this->session->userdata);