Welcome Guest, Not a member yet? Register   Sign In
Redirection Problem
#1

[eluser]Don 007[/eluser]
Hi I am reposting with more readable format code, 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
Code:
<?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


Code:
<?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..

#2

[eluser]InsiteFX[/eluser]
Code:
&lt;?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’);
        }
    }

}

Save it as ./application/core/MY_Controller.php

Code:
// CONTROLLER : Administration

&lt;?php // admin class
// This is not needed! require_once(‘restricted_area.php’);

class Administration extends Restricted_area {

    function __construct()
    {
        parent::__construct();
    }

}
#3

[eluser]Don 007[/eluser]
Hi thanks for the reply,
I want to ask here something, that as you suggest to put the restricted_area class in
application/core/MY_Controller.php, then here the class name and filename will be mismatch. And I think, the filename should match class name.

#4

[eluser]Don 007[/eluser]
Hi, Also I tried to put the code in ./application/core/MY_Controller.php and did the same way, but no success... still getting the same problem. Sad
#5

[eluser]meigwilym[/eluser]
Quote:the class name and filename will be mismatch

You're correct, but this will still work. Do exactly what InsiteFX has told you to do and it will work.

Mei
#6

[eluser]InsiteFX[/eluser]
It does not have to match it just has to be saved as MY_Controller the class name can be whatever you want!




Theme © iAndrew 2016 - Forum software by © MyBB