Welcome Guest, Not a member yet? Register   Sign In
Problem with user authentication
#1

[eluser]marcin_koss[/eluser]
I have a problem creating authentication part for my application.

Below is the simplified version of my controllers.

The idea is that the MY_controller checks if session with user data exists.
If it doesn't, then redirects to the index page where you have to log in.

MY_controller.php

Code:
class MY_Controller extends Controller {
    
function __construct()
{
parent::__construct();
        
$this->load->helper('url');
$this->load->library('session');
        
if($this->session->userdata('user') == FALSE) {
redirect('index');

} else {
redirect('search');
}

}

}

order.php - main controller

Code:
class Orders extends MY_Controller {

function __construct()
{
parent::__construct();
        
$this->load->helper('url');
$this->load->library('session');
}

function index()
{
// Here would be the code that validates information input by user.
// If validation is successful, it creates the user session.


$this->load->view('header.html', $data); // load header
$this->load->view('index_view', $data); // load body
$this->load->view('footer.html', $data); // load footer
}

function search()
{
//different page
}

what is happening is that the browser is telling me that "The page isn't redirecting properly. Firefox has detected that the server is redirecting the request for this address in a way that will never complete."

Does that mean that the redirect is in a loop?

I also came across this article where authentication is handled pretty much the same way
http://davidwinter.me.uk/articles/2009/0...deigniter/
#2

[eluser]Barry Cogan[/eluser]
Yes that does mean you are caught in an infinite loop.

A couple of things to note aswell:

you are already loading your URL and Session in the My_Controller. You don't need to load them them again in the Orders controller.

Secondly the parent call should read parent::MY_Controller(); for orders.php

and parent::Controller(); for MY_Controller.php

Makes for easier reading.
#3

[eluser]marcin_koss[/eluser]
Barry Cogan, I get this error after adding your corrections to the parent call.

"Fatal error: Call to undefined method MY_Controller::my_controller()"
#4

[eluser]marcin_koss[/eluser]
Ok, I fixed that. I also had to change names for constructors. So now again I'm back in the loop.

The way I called the parent method was fine according to OOP in PHP5.
#5

[eluser]Barry Cogan[/eluser]
I'm sure it is. It's more for ease of reading.

I assume you have the MY_Controller saved in the application/libraries directory?

I have never called multiple views in CI. I call a master view which has the view calls in it. Not sure if that has any bearing on the problem?
#6

[eluser]marcin_koss[/eluser]
Barry Cogan thanks for your help.

I solved the issue. I was calling redirect() function from within construct of the same controller which was causing the infinite loop. To make the login authentication work correctly I needed to create a new controller file I could redirect to when the user login session was not set.
#7

[eluser]Barry Cogan[/eluser]
Delighted you got it sorted. Good luck with your project! Smile




Theme © iAndrew 2016 - Forum software by © MyBB