dtjpz48440 2014-10-08 11:35
浏览 42
已采纳

在Codeigniter中检索会话数据

I am creating an application which requires a login. The user is able to login, and the application will identify if the user has entered the correct credentials. From here the user will be presented with a dashboard containing information specific to them.

Once a user has entered the correct details, the following code executes:

$this->load->library('session');
$this->session->set_userdata('logged_in', true);
redirect('dashboard', 'refresh');

The application will direct the user to the correct location, which contains the following code:

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

This will display the standard session information (session_id, ip_address etc.), but the data in which I stored is not shown.

How do I go about fixing this so that this session data is accessible on every page?

Any help would be greatly appreciated.

Edit:

Here are the current settings for sessions/cookies in config.php:

$config['sess_cookie_name']     = 'ci_session';
$config['sess_expiration']      = 7200;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie']  = TRUE;
$config['sess_use_database']    = TRUE;
$config['sess_table_name']      = 'ci_sessions';
$config['sess_match_ip']        = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update']  = 300;
$config['cookie_prefix']    = "";
$config['cookie_domain']    = "";
$config['cookie_path']      = "/";
$config['cookie_secure']    = FALSE;
  • 写回答

3条回答 默认 最新

  • douchui3933 2015-04-17 01:47
    关注

    When you log in to the controller, and if you are using CodeIgniter 2, you need to set sessions on the database:

    $config['sess_cookie_name']     = 'ci_session';
    $config['sess_expiration']      = 7200;
    $config['sess_expire_on_close'] = FALSE;
    $config['sess_encrypt_cookie']  = TRUE;
    $config['sess_use_database']    = TRUE;
    $config['sess_table_name']      = 'ci_sessions';
    $config['sess_match_ip']        = TRUE; // Change FALSE TO TRUE
    $config['sess_match_useragent'] = TRUE;
    $config['sess_time_to_update']  = 300;
    

    config/autoload.php

    $autoload['libraries'] = array(
        'database',
        'session'
    );
    

    In config/config.php, enter your own encryption_key

    $config['encryption_key'] = 'FN3ig7JhmCeE9mWFc1BOetIt603qcr81';
    

    Login

    public function index() {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required');
    
        if ($this->form_validation->run() == FALSE) {
            $this->load->view('common/login_view');
        }
        else {
            $set_userdata = array(
                'is_logged' => true,
                'username' => $this->input->post('username')
            );
            $this->session->set_userdata($set_userdata);
            redirect('success_page');
        }
    }
    

    On your parent part of controller.

    <?php
        class dashboard extends CI_Controller {
            public function __construct() {
                parent::__construct();
                if ($this->session->userdata('is_logged') == FALSE) {
                    redirect('your error page or login');
                }
            }
    
            public function index() {
    
            }
        }
    

    View Dashboard

    <pre>
        <?php print_r($this->session->all_userdata()); ?>
    </pre>
    
    Single
    <?php echo $this->session->userdata('username'); ?>
    

    I would recommend using CodeIgniter 3 now, http://www.codeigniter.com/.

    The other option is you could create a core/MY_Controller.php file:

    class MY_Controller extends CI_Controller {
    
        public function __construct() {
    
            parent::__construct();
            if ($this->session->userdata('is_logged') == FALSE) {
                redirect('your error page or login');
            }
        }
    }
    

    And then extend the controller you need sessions enabled:

    <?php
        class dashboard extends MY_Controller {
            public function __construct() {
                parent::__construct();
            }
    
            public function index() {
    
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化