0
votes

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

Here is the Controller code : 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');
}

Here is the view html file name : path views/login/home.php

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

THANKS IN ADVANCE

2

2 Answers

1
votes

Please go to application/config/autoload.php in your project directory and search libraries, after that add session to this line which will look like this.

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

In the __construct area you need to reload the session library using get instance in libraries

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);
    }
}