$_SESSION does not persist on other pages in the application

Asked

Viewed 575 times

3

good evening everyone,

I am using the codeigniter and native Session of php $_SESSION, it so happens that I can only use Session when I install a login controller that redirects to another page, but if I want to use the information of that Session on another page I can’t, says there is no such thing. I have already broken my head Ki I have tried everything a little but I can not continue in my application without first solving this question, I would like help from the staff who knows a little more. thank you all!

controller (this part works well and the admin view gets it easy to Session the problem eh if I want to use the information of this Session in other views informs q 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');
        }
    }


}

1 answer

1

So buddy, before you can create or take the value of a $_SESSION super variable, and need to first start the session session_start(), a single time on every page you need to create or take a $_SESSION.

Particularly I find it more advisable to use CI librarie Session.

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

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

and to take a Septssion would be,:

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

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

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

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

loading librarie into controller:

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

if you load a librarie in autoload, it will be available in the entire application, since if you load it in the controller, there it will be available only in the loaded controller, if you need to use it in another controller you will need to load it again.

your code would look like this using 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 see more details in the IC documentation: https://www.codeigniter.com/user_guide/libraries/sessions.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.