Logging in form_validation does not validate as expected

Asked

Viewed 125 times

0

I’m starting with codeigniter, but I have a little problem: When logging in, it doesn’t leave the incorrect password and email if. Form fields are correct and bank.

Can you help me?

This is the 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;

                    $session = array(
                                'user' => $usuario['email'],
                                'nome' => $usuario['nome'],
                                'logado' => true
                                );

                    $this->session->set_userdata($session);

                    redirect('administracao/index');

                }else{

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

                }

             }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');
    }

}

This 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->from('useradmin');
        $this->db->where('email', $email);
        $this->db->where('senha', $senha);
        $usuario = $this->db->get();
        print_r($usuario); die();

        if($usuario->num_rows() > 0){
            $user = $usuario->result_array();
            return $user[0];
        }else{
            return false;
        }
    }

}

which returned with print_r

CI_DB_mysqli_result Object ( [conn_id] => mysqli Object ( [affected_rows] => 0 [client_info] => mysqlnd 5.0.11-dev - 20120503 - $Id: f373ea5dd5538761406a8022a4b8a374418b240e $ [client_version] => 50011 [connect_errno] => 0 [connect_error] => [errno] => 0 [error] => [error_list] => Array ( ) [field_count] => 5 [host_info] => localhost via TCP/IP [info] => [insert_id] => 0 [server_info] => 5.6.21 [server_version] => 50621 [stat] => Uptime: 170781 Threads: 1 Questions: 3782 Slow queries: 0 Opens: 315 Flush tables: 1 Open tables: 106 Queries per second avg: 0.022 [sqlstate] => 00000 [protocol_version] => 10 [thread_id] => 634 [warning_count] => 0 ) [result_id] => mysqli_result Object ( [current_field] => 0 [field_count] => 5 [lengths] => [num_rows] => 0 [type] => 0 ) [result_array] => Array ( ) [result_object] => Array ( ) [custom_result_object] => Array ( ) [current_row] => 0 [num_rows] => [row_data] => )
  • it arrives to check if there is the Login??

  • Can you tell what stage the function is at? using print_r and die()??

  • Make intuitive titles to the problem, look at other questions on the site and follow their example, write HELP, HELP, PLEASE does not make the question more urgent, everyone see here needing help, write HELP, HELP, PLEASE, PLEASE is totally redundant, be objective while writing. Understand as a constructive criticism.

1 answer

0


If it helps you, I use this function to check the Login:

public function verificarLogin(){

    $this->load->library('form_validation');
    $this->form_validation->set_rules('email','Email','valid_email|required|xss_clean|trim');
    $this->form_validation->set_rules('senha','Senha','required|xss_clean|trim');
    $ajax = $this->input->get('ajax');
    if ($this->form_validation->run() == false) {

        if($ajax == true){
            $json = array('result' => false);
            echo json_encode($json);
        }
        else{
            $this->session->set_flashdata('error','Os dados de acesso estão incorretos.');
            redirect($this->login);
        }
    } 
    else {

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

        $this->load->library('encrypt');   
        $senha = $this->encrypt->sha1($senha);

        $this->db->where('email',$email);
        $this->db->where('senha',$senha);
        $this->db->where('status',1);
        $this->db->limit(1);
        $usuario = $this->db->get('usuario')->row();
        $permissao = $usuario->idPermissao;

        $sql = "SELECT * FROM permissoes WHERE idPermissao = '{$permissao}'";
        $permissao = $this->db->query($sql)->row('permissoes');



        if(count($usuario) > 0){
            $dados = array('nome' => $usuario->nome, 'empresa' =>  $usuario->empresa, 'id' => $usuario->idUsuario, 'idCliente' => $usuario->idCliente, 'permissao' => $usuario->idPermissao, 'permissoes' => $permissao, 'logado' => TRUE);
            $this->session->set_userdata($dados);

            if($ajax == true){
                $json = array('result' => true);
                echo json_encode($json);
            }
            else{
                redirect(base_url().'entrega');
            }


        }
        else{


            if($ajax == true){
                $json = array('result' => false);
                echo json_encode($json);
            }
            else{
                $this->session->set_flashdata('error','Os dados de acesso estão incorretos.');
                redirect($this->login);
            }
        }

    }

}

Browser other questions tagged

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