Error of Session in user level validation

Asked

Viewed 198 times

2

Good guys I am making a simple panel more with user level where I pass to the Session an array with three values, so far so good, the functions that validate the form, validates the bank user, whether the user is logged in or not, function that retrieves table data by validating by Session work perfectly more when I created the function that validates the user level to redirect each to his controller it displays the following error: Erro exibido pelo browser

login_controller.php

function __construct()
{
    parent::__construct();
    $this->load->helper(array('form', 'url'));
    $this->load->library('form_validation');
    $this->load->model('login_model');
    $this->load->model('crud_model');
}

public function index(){
    $this->load->view('login_view');
}


public function logando()
{
    $regras = array(
                array(
                    'field' => 'usuario_login',
                    'label' => 'Usuário',
                    'rules' => 'trim|required|min_length[3]|max_length[32]'
                    ),
                array(
                    'field' => 'senha_login',
                    'label' => 'Senha',
                    'rules' => 'trim|required|min_length[6]|max_length[32]'
                    )
                );

    $this->form_validation->set_rules($regras);
    if($this->form_validation->run() != true || $this->login_model->validate() == FALSE){
        $this->load->view('login_view');
    }else{
        redirect('admin/admin_controller');
    } } }

login_model.php

class Login_model extends CI_Model {

function validate()
{
    $campo['user'] = $this->input->post('usuario_login');
    $campo['pass'] = $this->input->post('senha_login');

    $where = array(
            'login_user'    =>  $campo['user'],
            'senha_user'    =>  md5($campo['pass']),
            'status_user'   =>  '1'
        );

    $this->db->where($where);
    $query = $this->db->get('usuarios');

    if($query->num_rows == 1)
    {
        $row = $query->row_array();//PEGA OS DADOS DO USUARIO COMPARADO COM OS CAMPOS

        $sess_validate  =   array(
            'session_logada'    =>  TRUE,//SE ELES ESTÁ LOGADO OU NÃO
            'session_user'      =>  $row['login_user'],//O NOME DE USUÁRIO QUE VEM DO BANCO
            'session_nivel'     =>  $row['nivel_user']//O NÍVEL QUE VEM DO BANCO
        );

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

        return TRUE;

    }else{
        $this->session->set_flashdata('user_erro_model', 'Usuário inexistente ou inativo');
        redirect(current_url());
        return FALSE;
    }
}

public function is_logado()
{
    if (trim($this->session->userdata('session_logada')) != TRUE)
    {
        redirect(index_page());
    }
}

public function is_nivel()
{
    if($this->session->userdata('session_nivel') == 1){
        redirect('admin/admin_controller');
    }else{
        redirect('user/user_controller');
    }
}

public function is_dados_user()
{
    $sess['user'] = $this->session->userdata('session_user');

    $where = array(
            'login_user'    =>  $sess['user']
        );

    $this->db->where($where);
    $query = $this->db->get('usuarios');

    $row = $query->row_array();

    if($query->num_rows > 0) return $row;

} }

admin_controller.php

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

class Admin_controller extends CI_Controller
{
function __construct()
{
    parent::__construct();
    $this->load->model('login_model');
    $this->login_model->is_logado();
    $this->login_model->is_nivel();
    $this->login_model->is_dados_user();
}

user_controller.php

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

class User_controller extends CI_Controller {

function __construct()
{
    parent::__construct();
    $this->load->model('login_model');
    $this->login_model->is_logado();
    $this->login_model->is_nivel();
    $this->login_model->is_dados_user();
}

public function index()
{
    $this->load->view('user');
}

public function sair()
{
    if($this->session->userdata('session_logada') == FALSE){
        redirect(index_page());
    }else{
        $this->session->sess_destroy();
        redirect(index_page());
    }
}

}

/* End of file user_controller.php */
/* Location: ./application/controllers/admin/user_controller.php */

I tried changing the settings of wamp server and nothing :(

  • Have you tried to see which of these redirects are causing the error? Try working with these redirects only in the controller and leave the models only for validation. So you will be able to identify who is causing the infinite loop. : Thin Controllers, Fat Models

1 answer

1


You always stay in redirect loop because in your constructs you call the function $this->login_model->is_dados_user(); in the Model and within that function, it calls the index as return.

The problem is that this function ALWAYS is executed called in the Construct of each CI class before the function index, causing this loop between the Builder and the Model.

I recommend changing the logic of validations, removing the functions with redirect(""); of the constructor and checking in the constructor class methods!

Ah, and use preferably Helpers for authentication validations and Models for your database data

  • 1

    My problems really were with infinite loops, I used a wrong logic in the validation and caused this, vlw men help a lot!

Browser other questions tagged

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