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

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)