Codeigniter继承现有的$ _SESSION

时间:2017-08-01 01:41:38

标签: php codeigniter session

我正在尝试从NONE codeigniter站点捕获现有会话,该站点有一个名为$_SESSION['username']的变量,现在当当前用户从该站点登录并尝试访问codeigniter站点时,控制器将只转移目前$_SESSIONset_userdata,目前为止我的代码是如此,但如果成功捕获print_r我没有运气,任何帮助都会很棒!

我正在使用$this->session->set_userdata

    <?php

    class Pages extends CI_Controller{

        public function view($page = 'home'){



                if(!file_exists(APPPATH.'views/pages/'.$page.'.php')){
                        show_404();
                }

        $this->session->set_userdata('CI_Username',$_SESSION['username']);           
        $data['user'] = $this->session->userdata('CI_Username');


                $data['title'] = ucfirst($page);

                $this->load->view('templates/header');
                $this->load->view('templates/nav');
                $this->load->view('pages/'.$page, $data);
                $this->load->view('templates/footer');

                print_r($data);

        }

}

2 个答案:

答案 0 :(得分:1)

如果您想获取会话数据,请尝试以下方法:

$data['user'] = $this->session->userdata('CI_Username');

由于

$this->session->set_userdata(...);

用于为会话分配值。

以下是链接:Link

答案 1 :(得分:0)

设置会话时

$sessionoptions = array('username', 'John');

$this->session->set_userdata($sessionoptions);

https://www.codeigniter.com/user_guide/libraries/sessions.html#adding-session-data

确保在config.php上设置了会话保存路径。

$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = APPPATH . 'cache/sessions/';
$config['sess_match_ip'] = TRUE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = TRUE;

设置会话文件夹权限0700。

然后

$data['user'] = $this->session->userdata('username');