accessibilty issue with codeigniter session library

时间:2017-07-06 17:10:47

标签: php codeigniter session

I'm working on user login system in codeigniter. When user clicks on login button the username and password are verified and the data is store in $this->session->set_userdata($arr); session library and the page redirects to home.php. Till here works fine but when is try to access the session in home.php as print_r($this->session->userdata);. it give me this errors

Error image

的颜色

这是Controller代码:Login.php

function login_user(){
    $login_btn = $this->input->post('login_btn');

    if($login_btn == TRUE){
        $arr['login'] = array(
            'username'=>$this->input->post('username'),
            'password'=>$this->input->post('password')
        );

        $query = $this->Login_model->users_login($arr);

        if($query->num_rows() > 0 && $query->num_rows() == 1){
            $this->load->library('session');
            $this->session->set_userdata($arr);
            redirect('Login/redirec_To_home','refresh');    
        }
        else
        {
            $this->index();
        }
    }
    else
    {
        $this->index();
    }
}

function redirec_To_home(){
    $this->load->view('login/home');
}

这是视图html文件名:path views / login / home.php

<?php
    print_r($this->session->userdata); //ERRORS SHOWN IN THIS LINE NUMBER : 13
?>

感谢提前

2 个答案:

答案 0 :(得分:1)

请转到项目目录中的application / config / autoload.php并搜索库,之后将会话添加到此行,如下所示。

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

答案 1 :(得分:0)

在__construct区域中,您需要使用库中的get实例重新加载会话库

https://www.codeigniter.com/user_guide/general/ancillary_classes.html

https://www.codeigniter.com/user_guide/general/creating_libraries.html#utilizing-codeigniter-resources-within-your-library

class Example {

    protected $CI;

    // We'll use a constructor, as you can't directly call a function
    // from a property definition.
    public function __construct()
    {
            // Assign the CodeIgniter super-object
            $this->CI =& get_instance();
            $this->CI->load->library('session');
            $this->CI->load->model('login_model');
    }

    public function somefunction()
    {
         $this->CI->input->post('username');
         $this->CI->input->post('password');

         $user_model = $this->CI->login_model->user_login();

         $sesstion_data = array('user_id' => '1');

         $this->CI->session->set_userdata($sesstion_data);
    }
}