codeigniter overrides session data from a non-ci app

时间:2019-04-05 07:15:24

标签: php codeigniter session codeigniter-3

I've two codes on the same domain. A legacy code works with a new codeigniter app.

Legacy code writes some data to $_SESSION that I need to use in codeigniter app.

If I use session_start() in codeigniter I can access to all $_SESSION data.

But if I use

$this->load->library('session');
var_dump($_SESSION);
var_dump($this->session->all_userdata());

all I see is

array(1) { ["__ci_last_regenerate"]=> int(1554448209) } 

Is there a way to use codeigniter session library and access session data from legacy code?

1 个答案:

答案 0 :(得分:0)

直接回答

-编辑开始

我意识到您需要采取相反的传统-> CI,我最初的回答是CI-> PHP。因此,您将必须在CI上设置session_id,我认为最好将钩子pre_controller与以下内容配合使用:

if(session_id() != $_GET['legacy-session-id']) {
    session_destroy();
    session_id($_GET['legacy-session-id']);
    session_start();
}

-编辑结束

CI会话配置

$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'sess_';
$config['sess_expiration'] = 3600;
$config['sess_save_path'] = sys_get_temp_dir();
$config['sess_match_ip'] = TRUE;
$config['sess_time_to_update'] = 600;
$config['sess_regenerate_destroy'] = FALSE;

sess_cookie_name必须为sess_sess_match_ip必须为FALSE,如果您在旧版PHP中使用默认的PHP会话驱动程序,则可能会这样做。

纯PHP

if(session_id()) session_destroy();
$CI_SESSION_ID = '2gmv4p3tfb8ds0btmi1m5um9nfei5lku'; // you'll have to get somehow
session_id( $CI_SESSION_ID );
//if sess_match_ip == true
//session_id( md5($_SERVER['REMOTE_ADDR']) . $CI_SESSION_ID );
session_save_path (sys_get_temp_dir());
session_start();

echo 'session_id = ' . session_id() . '<br />';
echo 'session_name = ' . session_name() . '<br />';
echo "session_save_path = " . session_save_path () . '<br />';
$_SESSION['my-test'] = time();
var_dump($_SESSION);

/* RESULT
session_module_name = files
session_id = f528764d624db129b32c21fbca0cb8d62gmv4p3tfb8ds0btmi1m5um9nfei5lku
session_name = PHPSESSID
session_save_path = C:\WINDOWS\TEMP
D:\Projetos\sis-main\teste.php:18:
array (size=2)
  '__ci_last_regenerate' => int 1554476877
  'my-test' => int 1554477377
*/

关于CI的结果:


echo 'session_id = ' . session_id() . '<br />';
echo 'session_name = ' . session_name() . '<br />';
echo "session_save_path = " . session_save_path () . '<br />';
var_dump($_SESSION);
/*
session_module_name = user
session_id = 5a82l70ra63p9bsch3rbhac1m4ekqppo
session_name = sess_
session_save_path = C:\WINDOWS\TEMP
D:\Projetos\sis-main\application\core\MY_Controller.class.php:22:
array (size=2)
  '__ci_last_regenerate' => int 1554477602
  'my-test' => int 1554477377
*/

说明

CodeIgniter使用自定义会话处理程序。通过一些工程学,我能够做到。

如果您查看CodeIgniter会话驱动器,将具有以下内容:

//@CI_Session_files_driver::open

$this->_file_path = $this->_config['save_path'].DIRECTORY_SEPARATOR
    .$name // we'll use the session cookie name as a prefix to avoid collisions
    .($this->_config['match_ip'] ? md5($_SERVER['REMOTE_ADDR']) : '');

如果从$name加载$config['sess_cookie_name']变量。而且,如果您已配置好设置IP地址,则文件名上将带有MD5。您不使用默认的PHP会话驱动程序。

因此,如果您具有此CI配置

$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'myproject_';
$config['sess_expiration'] = 3600;
$config['sess_save_path'] = sys_get_temp_dir();
$config['sess_match_ip'] = TRUE;
$config['sess_time_to_update'] = 600;
$config['sess_regenerate_destroy'] = FALSE;

您将在会话myproject_f528764d624db129b32c21fbca0cb8d62gmv4p3tfb8ds0btmi1m5um9nfei5lku中使用该文件名结束。

这是CI部分,现在让我们从PHP部分开始。

默认的PHP会话驱动器位于文件名“ sess_”之前。因此,您将得到以下内容:sess_ab0281624db129b32c21fbca0cb8d62。如果打开会话目录,则会看到它。

希望有帮助。