$ _SESSION does not persist on other pages in the application

1

Good evening guys,

I am using the codeigniter and native session of php $ _SESSION, it happens that I can only use the session when instancio in a login controller that redirects to another page, but if I want to use the information of this session in another page I can not , says qa session does not exist. I already broke my mind already, I tried a little bit of everything but I can not continue with my application without first solving this question, I would like the help of the staff who handle a little more. Thank you all!

controller (this part works fine and the admin view is calm to session the problem and if I want to use the information of this session in other views it does not 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');
        }
    }


}
    
asked by anonymous 14.03.2017 / 02:18

1 answer

0

Then friend, before you can create or get the value of a super variable $ _SESSION, and I need to first start the session session_start() , a single time on each page that needs to be created or get a $ _SESSION. p>

Particularly I find it more advisable to use the librarie session of CI.

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

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

and to get a session, it would be:

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

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

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

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

loading librarie in the controller:

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

If you load a librarie into autoload, it will be available throughout the application, if you load it into 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 librarie:

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 check more details in the CI documentation: link

    
21.04.2017 / 18:57