php 在 CodeIgniter 中设置 Session_id

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6656491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:55:37  来源:igfitidea点击:

Setting Session_id in CodeIgniter

phpsessioncodeigniter

提问by Roman

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

在标准 PHP 中,我可以通过以下方式设置会话 ID:

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

我如何使用 CodeIgniter 做同样的事情?我正在尝试以下操作:

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

我犯了一些错误吗?请帮助我,因为我在这个问题上浪费了几个小时。如果 CodeIgniter Session 类有问题,我可以使用标准的 PHP 代码来启动会话吗?我也尝试将标准代码放在 CodeIgniter 中,但它仍然没有设置 session_id。我也设置了$config['sess_match_useragent'] = FALSE;

采纳答案by cwallenpoole

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

这是我刚刚在另一个问题上建议的一个技巧。将其放在__constructof MY_Session(未经测试且不适用于加密)

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

回答by Eddie

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

您不需要这样做,codeigniter 会为您完成这一切。

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

如果您想获取会话 ID,可以通过调用:

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

但是你可以解决它:(注意这篇文章已经 3 年了,我不确定它是否仍然有必要)

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

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

回答by DonSeba

use this for setting session_id in codeigniter:

使用它在 codeigniter 中设置 session_id:

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

and

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

for reading it out again.

再读一遍。

回答by IOInterrupt

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

在修改任何会话变量之前,您应该始终启动会话。

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

我相信要启动 CodeIgniter 会话,您可以执行以下操作;

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