Welcome Guest, Not a member yet? Register   Sign In
Basic authentication system
#1

[eluser]Huji[/eluser]
I've successfully used Simplelogin library to setup a basic authentication system within a few minutes. I have several functions in my controller, and in the beginning of most of them, I have to check if the user is logged in like this:

function Blah(){
if(!$this->session->userdata('logged_in')) {
redirect('/login/');
} else {
// DO WHATEVER YOU DO
}
}

I would like to know if there is a way to get rid of this if statement? Like by extending from a base function, etc?
#2

[eluser]Dam1an[/eluser]
If all functions in that conroller require you to be logged in, you can put the check in the contrustor
If all (or almost all) pages accross all controllers require you to be logged in, you could move t into the MY_Controller constuctor

Also, can you please use [ code ] tags when posting code in the future
#3

[eluser]TheFuzzy0ne[/eluser]
Sure, you could [url="http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html"]extend the controller[/url] class, or you could export the functionality to a [url="http://ellislab.com/codeigniter/user-guide/general/libraries.html"]library[/url] or [url="http://ellislab.com/codeigniter/user-guide/general/helpers.html"]helper[/url].
#4

[eluser]Huji[/eluser]
Dam1an,

All functions in the controller require the user to be logged in with the exception of login() function. Could you please show me how I can move the check into MY_Controller constructor? (The second sentence you said), while preserving the functionality of login() function?

I will surely use [C‌ode] tag in future.
#5

[eluser]n0xie[/eluser]
Code:
class MY_Controller extends Controller
{

  function MY_Controller()
  {
    parent::Controller();
    if( ! $this->session->userdata(‘logged_in’))
    {
      redirect(’/login/’);
    }
  }
}

class Blog extends MY_Controller
{

  function Blog()
  {
    parent::MY_Controller();
  }
}

Just make sure your controller 'login' does NOT extend MY_Controller else you will have an endless loop Wink
#6

[eluser]TheFuzzy0ne[/eluser]
Everything you need to know is in the [url="http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html"]user guide[/url].

Here's a link to my [url="http://ellislab.com/forums/viewthread/107773/"]CodeIgniter Resources[/url] thread. I think you might find it useful.
#7

[eluser]Dam1an[/eluser]
I would change the if clause to be
Code:
if(!$this->session->userdata('logged_in') && $this->uri->uri_string() != 'login') {
    redirect('login');
}

You can then extend all controllers off it
If you end up with a few functions which don't require you to be logged in, you could have an array, and check if the URI string is in that array
#8

[eluser]Huji[/eluser]
Thanks for both replies. Sorry if I sound like a dummy, but I'm still not convinced by the answers. I don't have a "login" controller. Login is a function of the controller. I was trying to ask if there was a way to make sure all functions of a controller (with the exception of one) would run the authentication check in the beginning.

Please advise.
#9

[eluser]TheFuzzy0ne[/eluser]
What dishes up the login form to the user? That should be your login controller, which can then call on the login method in your auth library (or in your case, your controller, which is not the right place for it).
#10

[eluser]Dam1an[/eluser]
In my example, just change the second part of the condition to be whatever URI string you use to access the login form
In all of my cases, I always route 'login' to authentication (controller) / login (function) so the URI string is always going to be login in my case




Theme © iAndrew 2016 - Forum software by © MyBB