Search code examples
phpcodeignitersessioncontrollerradix

Session data ci3 base controller


I have this on my controller named pages(i use it as a terminal to branch out to all views)

class pages extends MY_Controller 
{
    public function verification()
    {
        $session_data = $this->is_logged_in();

        print_r($session_data);

        if($this->is_logged_in())
        {

        }
    }
}

As you can see, i have a line 'print_r' on the session_data, i got some results the problem is it always returns only '1'.

This is the function on my base controller.

class MY_Controller extends CI_Controller 
{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function is_logged_in()
    {
        $user = $this->session->userdata();
        return isset($user);
    }  
}

And this is the code block after logging in creating the session data on my authenticator controller.

Class user_authentication extends MY_Controller 
{
    public function register()
    {
        $this->form_validation->set_rules('username', 'Username','required|trim|min_length[8]|max_length[24]|is_unique[user.username]', [
            'required' => 'You have not provided %s.', 
            'is_unique' => 'This %s already exists.'
        ]);    
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[8]|max_length[16]');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|min_length[6]|max_length[30]|is_unique[user.email]', [
            'required' => 'You have not provided %s. ', 
            'is_unique' => 'This %s already exists.'
        ]);
            $this->form_validation->set_rules('type', 'Account Type', 'trim|required|min_length[1]|max_length[1]', [
            'required' => 'please select: 1 - normal, 2 - trainer'
        ]);
        
        $this->form_validation->set_error_delimiters('', '');

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('templates/header');
            $this->load->view('templates/navigation');
            $this->load->view('pages/login');
            $this->load->view('templates/footer');
        }
        else
        {
            $data = [
                'username' => $this->input->post('username'),
                'password' => $this->input->post('password'),
                'email' => $this->input->post('email'),
                'type' => $this->input->post('type'),
            ]; 
            
            $result = $this->Account_model->create_account($data);

            if($result == true)
            {
                $data['message_display'] = 'Registration Successful';

                $this->load->view('templates/header');
                $this->load->view('templates/navigation');
                $this->load->view('pages/homepage',$data);
                $this->load->view('templates/footer');    
            }
            else
            {
                $data['message_display'] = 'Username and/or Email already exists';

                $this->load->view('templates/header');
                $this->load->view('templates/navigation');
                $this->load->view('pages/login',$data);
                $this->load->view('templates/footer');
            }
        }
    }
    
    public function login()
    {
        $this->form_validation->set_rules('username', 'Username', 'required|trim|min_length[8]|max_length[24]');
        $this->form_validation->set_rules('password', 'Password', 'required|trim|min_length[8]|max_length[16]');

        if($this->form_validation->run() == FALSE)
        {
            $this->load->view('templates/header');
            $this->load->view('templates/navigation');
            $this->load->view('pages/login');
            $this->load->view('templates/footer');
        }
        else
        {
            $data = [
                'username' => $this->input->post('username'), 
                'password' => $this->input->post('password')
            ];
            $result = $this->Account_model->login_account($data);
            
            $this->session->set_userdata('isloggedin', [
                'username' => $result['username'], 
                'email' => $result['email'], 
                'type' => $result['type']
            ]);

            $data['title'] = 'home';

            $this->load->view('templates/header');
            $this->load->view('templates/navigation');
            $this->load->view('pages/home');
            $this->load->view('templates/footer');
        }
    }
}

There are some stuff that are basically useless as of right now but i'm putting them there for future uses. like the $data['title'] gonna use it as a placeholder for the title on the head of each view since i am using templates.

Am i doing something wrong? Why does when i print_r the session data from MY_Controller it only returns '1'.

I've been looking at guides but most of them are outdated and i am probably using deprecated stuff from past versions, if so kindly point them out, i want to practice coding neat and clean.


Solution

  • Your approach won't work that way because userdata returns an array with at least one key called __ci_last_regenerate (i'm not sure which CI version you use but if you autoload the session library you would always get an result here)

    Set an extra key isLoggedin and you'll be fine.

    Your login function:

    public function login()
    {
        $this->form_validation->set_rules('username', 'Username', 'required|trim|min_length[8]|max_length[24]');
        $this->form_validation->set_rules('password', 'Password', 'required|trim|min_length[8]|max_length[16]');
        if($this->form_validation->run() == FALSE)
        {
            $this->load->view('templates/header');
            $this->load->view('templates/navigation');
            $this->load->view('pages/login');
            $this->load->view('templates/footer');}
        else
        {
            $data = array('username' => $this->input->post('username'), 'password' => $this->input->post('password'));
            $result = $this->Account_model->login_account($data);
    
            if ($result)
            {
                $session_data = array('username' => $result['username'], 'email' => $result['email'], 'type' => $result['type'], 'isLoggedin' => true);
                $this->session->set_userdata($session_data);
    
                $data['title'] = 'home';
                $this->load->view('templates/header');
                $this->load->view('templates/navigation');
                $this->load->view('pages/home');
                $this->load->view('templates/footer');
    
            }
    
        }
    }
    

    and your controller

    class MY_Controller extends CI_Controller {
        public function __construct()
        {
            parent::__construct();
        }
    
        public function is_logged_in()
        {
            return $this->session->userdata('isLoggedin');
        }  
    }
    

    With this approach you get either a correct value or NULL which returns true or false;

    For more information you can take a look here https://codeigniter.com/userguide3/libraries/sessions.html#CI_Session::userdata