Treat session by codeigniter model or controller

Asked

Viewed 444 times

1

I’m trying to send the data from the database to the session, gave a print_r on the data received from the bank, but I can not pass it to the session, follow the code.

Controller

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class login extends CI_Controller {

public function entrar(){

    $mensagem = null;

    if($this->input->post('acessar') === 'acessar'){

         $this->form_validation->set_rules('user', 'email', 'required|valid_email');
         $this->form_validation->set_rules('senha', 'senha', 'required|min_length[5]|max_length[40]');

         if($this->form_validation->run() === true){

            $this->load->model('LoginModel');

            $email = $this->input->post('user');
            $senha = md5($this->input->post('senha'));

            $loginExistente = $this->LoginModel->verificaLogin($email,$senha);

            if($loginExistente === true){

                $usuario = $loginExistente;

                redirect('administracao/index');

            }else{

                $mensagem = array('class' => 'danger',
                                'mensagem' => 'Login inválido, e-mail ou senha incorretos.'
                            );

            }

         }else{

            $mensagem = array('class' => 'danger',
                                'mensagem' => 'Foram encontrados erros no login </br>'. validation_errors()
                            );
         }
    }

    $dados = array('alerta' => $mensagem);

    $this->load->view('login/index', $dados);
}

public function sair(){
    $this->session->sess_destroy();

    redirect('login/entrar');
}

}

Here is the model

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class LoginModel extends CI_Model {

    public function verificaLogin($email,$senha){


        $this->db->where('email', $email);
        $this->db->where('senha', $senha);
        $usuario = $this->db->get('useradmin')->row();
        $total = count($usuario);

        print_r($usuario); die();
        if($total == 1){

            $data = array(
                    'id' => $usuario->id,
                    'nome' => $usuario->nome,
                    'email' => $usuario->email,
                    'nivel' => $usuario->nivel,
                    'logado' => true
                    );

            $this->session->set_userdata($data);
            /*$user = $usuario->result_array();*/
            /*return $user[0];*/
            return true;
        }else{
            return false;
        }
    }

}

and that brings when I give a print_r and try to log in

stdClass Object ( [id] => 1 [nome] => Débora [email] => [email protected] [senha] => 698dc19d489c4e4db73e28a713eab07b [nivel] => 1 ) 

Could you help me treat this, please?

  • Hello, Login not working yet? Do not pass the data?

  • 3

    you don’t answer the comments... then it’s hard to help you little friend!

1 answer

1

Looks like you didn’t start the session.

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

Try this, to debug what is saved in the session, do the print_r of

$this->session->all_userdata()

Browser other questions tagged

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