avoid adding authentication on each 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: avoid adding authentication on each function (/showthread.php?tid=32482) |
avoid adding authentication on each function - El Forum - 07-25-2010 [eluser]jtan[/eluser] is there a way to avoid adding authentication code on each function in Codeigniter? I ask because virtually all functions require authentication. what the function does it route to login page when it's not authenticated. Thanks. avoid adding authentication on each function - El Forum - 07-25-2010 [eluser]WanWizard[/eluser] Create a MY_Controller, and do your check in the constructor of that controller. avoid adding authentication on each function - El Forum - 07-25-2010 [eluser]jtan[/eluser] great, got it working, i now put all controllers to use MY_Controller and only the login page not using MY_Controller to avoid the endless loop avoid adding authentication on each function - El Forum - 07-25-2010 [eluser]WanWizard[/eluser] That works as long as you don't do other things in your MY_Controller that need to be available everywhere (including your login controller). It's better to check the controller being called, and redirect if it's not the login controller: Code: if ( $this->router->fetch_class() != 'login' && $this->router->fetch_method() != 'auth' ) avoid adding authentication on each function - El Forum - 07-25-2010 [eluser]jtan[/eluser] here's what I have function __construct() { parent::Controller(); if($this->session->userdata('authenticated')==0) { redirect('login'); } if(is_null($this->session->userdata('authenticated'))) { redirect('login'); } } avoid adding authentication on each function - El Forum - 07-25-2010 [eluser]WanWizard[/eluser] I assume that works to. You can do Code: if ( empty($this->session->userdata(‘authenticated’)) ) avoid adding authentication on each function - El Forum - 07-25-2010 [eluser]jtan[/eluser] great, thanks. avoid adding authentication on each function - El Forum - 07-25-2010 [eluser]jtan[/eluser] strange, I used your code and it returns empty, did you use any library for the "empty" command avoid adding authentication on each function - El Forum - 07-25-2010 [eluser]WanWizard[/eluser] The userdata() method of the session class returns FALSE if the variable does not exist, otherwise it returns the stored value. I think the issue is that empty() is a language construct, you might not be able to use it on a method call. In that case (assuming that you store 0 and 1), you can just use Code: if ( ! $this->session->userdata(‘authenticated’) ) avoid adding authentication on each function - El Forum - 12-25-2010 [eluser]jtan[/eluser] is it possible to share the code across several controllers instead of just functions within the controller? |