CodeIgniter Forums
Problem with user authentication - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Problem with user authentication (/showthread.php?tid=30600)



Problem with user authentication - El Forum - 05-19-2010

[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/02/21/authentication-with-codeigniter/


Problem with user authentication - El Forum - 05-19-2010

[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.


Problem with user authentication - El Forum - 05-19-2010

[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()"


Problem with user authentication - El Forum - 05-19-2010

[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.


Problem with user authentication - El Forum - 05-20-2010

[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?


Problem with user authentication - El Forum - 05-20-2010

[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.


Problem with user authentication - El Forum - 05-21-2010

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