Redirect Loop error in CodeIgniter

user2103279 picture user2103279 · Feb 23, 2013 · Viewed 10.3k times · Source

I worked on this for a day. I get this same problem, but I don't understand.

<?php
class Users extends CI_Controller
{
    function index()
    {
        redirect('Users/login');
    }

    function login()
    {
        $data['title']        = 'Selamat datang di Sistem Informasi Koperasi';
        $data['main_content'] = 'login';
        $this->load->view('Users/template', $data);
    }

    function logout()
    {
        $this->session->sess_destroy();
        $this->session->set_flashdata('info_login', 'Anda sudah keluar dari sistem');
        redirect('Users/login');
    }

    function validate()
    {
        //Load User Model
        $this->load->model('Users_Model');
        //Validate User
        $query = $this->Users_Model->validate();


        if($query != '') {
            //Mengambil Roles dari Groups
            $roles = $this->Users_Model->roles($query->group_id);

            //$this->login_model->last_login($query);
            $data = array(
                'username' => $query->username,
                'roles' => $roles,
                'is_logged_in' => true
            );
            $this->session->set_userdata($data);

            if($roles == 'administrators') {
                redirect('Administrators/index');
            } elseif($roles == 'managers') {
                redirect('Managers/index');
            }

            else {
                $this->session->set_flashdata('info_login', 'Mohon maaf anda belum terdaftar sebagai Group! Silahkan hubungi admin!');
                redirect('Users/login');
            }
        } else {
            $this->session->set_flashdata('info_login', 'Maaf,username dan password yang anda masukkan salah,silahkan coba kembali!');
            redirect('Users/login');
        }
    }

}

In Chrome and Firefox I get this message. What should i do?

This webpage has a redirect loop The webpage at http://localhost/simpks/index.php/Users/login has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer. Here are some suggestions: Reload this webpage later. Learn more about this problem. Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many redirects.

this is my view template.php

<?php
$this->load->view('includes/header',$main_content); 
$this->load->view('Users/'.$main_content);
$this->load->view('includes/footer');
?>

this is my model Users_Model.php

    <?php

    class Users_Model extends CI_Model{

        function validate(){    
            $this->db->where('username',$this->input->post('username'));
            $this->db->where('password',md5($this->input->post('password')));
            $query = $this->db->get('Users');

            if($query->num_rows == 1){
                $row = $query->row();
                return $row;
            }
        }

        function roles($id){
            $this->db->where('id',$id);
            $query = $this->db->get('Groups');

            if($query->num_rows == 1){
                $row = $query->row();
                return $row->name;
            }
        }

    }

?>

Answer

Ramadhan Taufiq picture Ramadhan Taufiq · Feb 24, 2013

use include instead loader if you call it in view. ex : include 'includes/footer';