Welcome Guest, Not a member yet? Register   Sign In
Authorize Users in Main Controller Function?
#1

[eluser]ShoeLace1291[/eluser]
Ok, so I've been experimenting with the main controller functions(i.e. Welcome()) and I figured I'd authenticate my users once at the top of the controller. The only problem I ran into is that since the if statement only occurs in the main function, the normal function is still displayed.

For example, if the user visits the index function of the current controller and isn't logged in, the contents of the index function are still displayed, even though the authentication message is there.

Is there a way to stop this?

Example code:
Code:
class Home extends Controller {

     function Home(){
          parent::Controller();
          if($this->member->isLogged == FALSE){
               $this->load->view('auth_error');
          }
     }

     function index(){
           $this->load->view('index_body');
     }

so the problem is, even if the isLogged variable is false, it loads the auth_error view AND the index_body view, and I only want it to load the auth_error view in the case that the user is logged out.

Any ideas?
#2

[eluser]bassneck[/eluser]
Well, sure it executes index function after the constructor since you do nothing to prevent this.
To stop script from execution you could make something like this:
Code:
if($this->member->isLogged == FALSE){
     $this->load->view('auth_error');
     die();
}

Another option would be to use _remap(). More info on this is available in the controller section of the user guide
#3

[eluser]techgnome[/eluser]
or use redirect() ... or set a flag in the main function, check for it in the other functions and load the appropriate view from there.

-tg
#4

[eluser]coolfactor[/eluser]
You don't want to be display views in the constructor method. That method is just for _constructing_ the controller object. Leave the view work up to the controller's other methods. So if the user is not authenticated, don't even let them get to the method... do a redirect to a method that displays a login form, for example.




Theme © iAndrew 2016 - Forum software by © MyBB