Customize data from a codeigniter session array

Asked

Viewed 455 times

0

I am trying to create an array with the data I recover 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 make the query in the bank and retrieve the information in an array and save in $login. Dai 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 procedure, I check if the user and password are correct in the bank and authorize the login, if wrong, I report the error. Now I wanted to pass some data to be able to use in the future within the application, 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, what I do so that these variables receive the data that was recovered from the bd.

PS: My DB is 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);
  • Okay, I’ve noticed that the problem ta being the syntax of $this->input->post('...'), but I’m not finding the correct syntax.

  • 1

    Change $this->input->post('id') for $login[0]['id'] and $this->input->post('tipo') for $login[0]['tipo'] you are not passing these values via form but recovering from your query in the database, so just take the variable that brings this data and for them in its function that creates the session.

  • @William Novak this way gave the error 'Function name must be a string'. The variable did not receive the database data. As I commented, I know the problem is being this statement, but as I’m not very good with php, I’m having this difficulty.

  • Gives a print_r($login) and see if the variable is really taking the database data by your query.

  • @William Novak I just did the test here, and the $login variable is not getting any data really, so I’m not getting the user data back.

1 answer

2


I was able to solve the problem, created a model where I did the query and returned the data, so I filled in the session array. So the Controller was:

$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 so:

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 too.

  • I think my contribution was worth nothing, not even a little bit of the people you saw...

Browser other questions tagged

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