Customizing data from a session array in codeigniter

0

I'm trying to create an array with the data I retrieve from the database, my code is like this

$usuario = $this->input->post("usuario");
$senha = $this->input->post("senha");
$this->db->select('id','usuario','senha','tipo');
$this->db->from('usuario');
$this->db->where('usuario',$usuario);
$this->db->where('senha',MD5($senha));
$login = $this->db->get()->result();

Here I query the database and retrieve the information in an array and save it in $ login. So I check if this array is not empty to authenticate the session

 if ( is_array($login) && count($login) == 1) {
      $this->session->set_userdata("logado", 1);
      $this->load->view("admin/redireciona");
    } else {
//caso a senha/usuário estejam incorretos, então mando o usuário novamente para a tela de login com uma mensagem de erro.
        $dados['erro'] = "Usuário/Senha incorretos";
        $this->load->view("login", $dados);
    }

When doing this, I check if the user and password are correct in the bank and I authorize the login, in case of wrong, I inform the error. Now I wanted to pass some data to be able to use it in the future, and that's where I'm having problems.

$dadossessao = array(
            'id' => $this->input->post('id'),
            'nome' => $this->input->post('usuario'),
            'tipo' => $this->input->post('tipo'),
            'logado' => 1
);

The 'id' and 'type' are not returning values, which I do for these variables to receive the data that was retrieved from bd.

PS: My DB looks like this:

CREATE TABLE 'usuario' (
'id' int(11) NOT NULL,
'usuario' varchar(100) NOT NULL,
'senha' varchar(100) NOT NULL,
'tipo' int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO 'usuario' ('id', 'usuario', 'senha', 'tipo') VALUES
(1, 'teste', MD5('teste'), 1);
    
asked by anonymous 11.07.2016 / 22:14

1 answer

0

I was able to solve the problem, I created a model where I made the query and it returned the data to me, from there I filled the array of the session. This is the Controller:

$this->load->model('login');
    $usuario = $this->input->post("usuario");
    $senha = MD5($this->input->post("senha"));
    $login = $this->login->autenticar($usuario, $senha);

    if ($login) {
        $dados = array(
            'logado' => 1,
            'usuario' => $login['usuario'],
            'tipo' => $login['tipo'],
            'id' => $login['id']
        );
        $this->session->set_userdata($dados);
        $this->load->view("admin/redireciona");
    } else {
        $dados['erro'] = "Usuário/Senha incorretos";
        $this->load->view("login", $dados);
    }

And the model like this:

public function autenticar($usuario, $senha){

    $this->db->where('usuario', $usuario);
    $this->db->where('senha', $senha);
    $log = $this->db->get('usuario')->row_array();
    return $log;

}

I hope it helps others who have this problem as well.

    
18.07.2016 / 13:38