codeigniter – $_SESSION does not persist on other pages in the application

Question:

Good night people,

I'm using the codeigniter and native session from php $_SESSION, it turns out I can only use the session when I instantiate in a login controller that redirects to another page, but if I want to use the information from this session on another page I can't, it says qa session does not exist. I've already racked my brain aki, I've tried a bit of everything but I can't proceed with my application without first solving this issue, I would really like the help of the guys who know a little more. thank you all!

controller (this part works great and the admin view calmly receives the session the problem is if I want to use the information from this session in other views it says that it doesn't exist)

public function check_login()
{
    $login = $this->input->post('login');
    $pass =  md5(md5(md5($this->input->post('pass'))));

    $user = $this->login_model->checkLogin($login, $pass);

    if(!$user)
    {

        $data['msg_erro'] = "Login ou senha inválido.";
        $this->load->view('index', $data);


    }
    else{

        if($user->cod_user == 1)
        {


            $_SESSION['ci_session'] = $user->name;
            $this->load->view('admin');


        }
        else if($user->cod_user == 2)
        {
            $_SESSION['vendedor'] = $user->name;

            $this->load->view('vendedor');
        }
    }


}

Answer:

So friend, before you can create or get the value of a super variable $_SESSION, I need to first start the session_start() session, only once on each page that it is necessary to create or get a $_SESSION.

Particularly I think it's more recommendable to use the CI librarie session.

for you to create a session with the CI librarie session, it would be:

$this->session->set_userdata('nameSession', 'valueSession');

and to take a session, it would be:

$this->session->userdata('nameSession');

remembering that you must load the librarie before using, which can be done both in autoload and in the controller.

loading librarie into CI autoload: Application/config/autoload.php

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

loading librarie into the controller:

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

if you load a librarie on autoload, it will be available throughout the application, if you load it on the controller, it will only be available on the loaded controller, if you need to use it on another controller you will need to load it again.

your code would look like this using the CI library:

public function check_login()
{
    $login = $this->input->post('login');
    $pass =  md5(md5(md5($this->input->post('pass'))));

    $user = $this->login_model->checkLogin($login, $pass);

    if(!$user)
    {
        $data['msg_erro'] = "Login ou senha inválido.";
        $this->load->view('index', $data);
    }
    else{

        if($user->cod_user == 1)
        {   
            $session['admin'] = $user->name;
            $this->session->set_userdata($session);
            $this->load->view('admin');
        }
        else if($user->cod_user == 2)
        {
            $session['vendedor'] = $user->name;
            $this->session->set_userdata($session);
            $this->load->view('vendedor');
        }
    }
}

you can see more details in the CI documentation: https://www.codeigniter.com/user_guide/libraries/sessions.html

Scroll to Top