开发者

Setting Session_id in CodeIgniter

开发者 https://www.devze.com 2023-03-19 06:45 出处:网络
In standard PHP i can set the session ID by fo开发者_StackOverflowllowing: $my_session_id = $_GET[\'session_id\']; //gets the session ID successfully

In standard PHP i can set the session ID by fo开发者_StackOverflowllowing:

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


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/


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()
}


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号