![]() |
Authorize Users in Main Controller Function? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: Authorize Users in Main Controller Function? (/showthread.php?tid=34731) |
Authorize Users in Main Controller Function? - El Forum - 10-07-2010 [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 { 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? Authorize Users in Main Controller Function? - El Forum - 10-08-2010 [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){ Another option would be to use _remap(). More info on this is available in the controller section of the user guide Authorize Users in Main Controller Function? - El Forum - 10-08-2010 [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 Authorize Users in Main Controller Function? - El Forum - 10-08-2010 [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. |