[eluser]Don 007[/eluser]
Hi there, I am creating a login form, for admin section. What I have done here, I have created a controller
restricted_area which serves as security for user. It is extend by other class in admin. Below is code for each controller class I am using.
CONTROLLER : restricted_area
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Restricted_area extends CI_Controller
{
function __construct($module_id = null)
{
parent::__construct();
if($this->session->userdata('user_id'))
{
redirect('administration');
}
else
{
redirect('administration/login');
}
}
}
CONTROLLER : Administration
<?php // admin class
require_once('restricted_area.php');
class Administration extends Restricted_area
{
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->view('admin/dashboard');
}
function login()
{
$this->form_validation->set_rules('username','lang:login_username','callback_login_check');
$this->form_validation->set_error_delimiters('<div class="error">','</div>');
if($this->form_validation->run() == FALSE)
{
$this->load->view('admin/login');
}
else
{
redirect('administration');
}
}
function login_check($username)
{
$password = $this->input->post("password");
if(!$this->User->login($username,$password))
{
$this->form_validation->set_message('login_check',$this->lang->line('login_invalid_username_and_password'));
return false;
}
return true;
}
function logout()
{
$this->User->logout();
redirect('administration/login');
}
}
The basic logic to use restricted_area class is that when the user data is available
in session i.e. if he is logged in, he get redirected to dashboard, and if he is not logged in if he put url for dashboard he get redirected to login page.
when I am running this code, Mozilla display : This page cannot redirect properly, and Chrome display : This webpage has a redirect loop, too many redirects.
When I remove restricted_area class and run the administration class by extending CI_Controller, and write those codes in the functions, it works fine. But with this approach, I have to write the session user data check in every class or function for admin panel. Thats why I created a class restricted_area so that it get extended by all the class in admin panel so its session security get available to all. But redirection problem occurs.
Any suggestion..