Welcome Guest, Not a member yet? Register   Sign In
Check for Login on Each Function?
#1

[eluser]igul222[/eluser]
Hi. I have several controllers in a CI app, each with several more functions. Almost all of these need to check if the user is logged in, and send him/her back to an error page if not. The result is that my code for about 30 functions (5 controllers, 6 functions each) looks like this:
Code:
if($this->session->userdata('logged_in') != 'yes')
     $this->load->view('not_logged_in');
else
{
     //the rest of my function's code here
}
Is there any way to do this without manually putting this block of code everywhere (e.g. in the controller's constructor)?
#2

[eluser]sophistry[/eluser]
put the code in the controller's constructor. or sub-class.
#3

[eluser]BizComputing[/eluser]
I have a similar issue.

I created a Controller extension class where I could add all my code that needs to execute for all my controllers. When creating controllers, I extend my controller class rather than Controller.
#4

[eluser]Tom Glover[/eluser]
Solution Controller

Code:
class Example extends Controller {
    
    function Example()
    {
         parent::Controller();
         if($this->session->userdata('logged_in') != 'yes'){
         $this->load->view('not_logged_in'); }
         //logged in load other code
         // the above code is loaded for ever function inside the controller
    }
          // your functions here
    function index()
    {
          // index function here
    }
}

Read the above comments and it should be self explanatory.
#5

[eluser]igul222[/eluser]
[quote author="WackyWebs.net" date="1201902614"]Solution Controller

Code:
class Example extends Controller {
    
    function Example()
    {
         parent::Controller();
         if($this->session->userdata('logged_in') != 'yes'){
         $this->load->view('not_logged_in'); }
         //logged in load other code
         // the above code is loaded for ever function inside the controller
    }
          // your functions here
    function index()
    {
          // index function here
    }
}

Read the above comments and it should be self explanatory.[/quote]
Thanks! However, I need to actually prevent the code inside index() from being run. Example:
Code:
class Example extends Controller {
    
    function Example()
    {
         parent::Controller();
         if($this->session->userdata('logged_in') != 'yes'){
         $this->load->view('not_logged_in'); }
         //logged in load other code
         // the above code is loaded for ever function inside the controller
    }
          // your functions here
    function index()
    {
          //i really don't want this code to be run...
          $this->db->insert('mytable',$some_dangerous_stuff);
          $this->load->view('user_profile');
    }

Is there any way to do that?
#6

[eluser]sophistry[/eluser]
class variable?

var $logged_in;
#7

[eluser]igul222[/eluser]
[quote author="sophistry" date="1201904477"]class variable?

var $logged_in;[/quote]

*hits himself in head*

Thanks!
#8

[eluser]Tom Glover[/eluser]
[quote author="igul222" date="1201904162"][quote author="WackyWebs.net" date="1201902614"]Solution Controller
Code:
class Example extends Controller {
    
    function Example()
    {
         parent::Controller();
         if($this->session->userdata('logged_in') != 'yes'){
         $this->load->view('not_logged_in'); }
         //logged in load other code
         // the above code is loaded for ever function inside the controller
    }
          // your functions here
    function index()
    {
          //i really don't want this code to be run...
          $this->db->insert('mytable',$some_dangerous_stuff);
          $this->load->view('user_profile');
    }

Is there any way to do that?[/quote]
I Think this might work
Code:
class Example extends Controller {
    
    function Example()
    {
         parent::Controller();
         if($this->session->userdata('logged_in') != 'yes'){
         //$this->load->view('not_logged_in'); //old code replace with a redirect to no access controller
$this->load->helper('url')
$no_access =site_url('no_access/forbidden')
header( "Location: $no_access " ); //Should Work, if not try this: header( 'Location: '.$no_access  );
        }
         //logged in load other code
         // the above code is loaded for ever function inside the controller
    }
          // your functions here
    function index()
    {
          //i really don't want this code to be run...
          $this->db->insert('mytable',$some_dangerous_stuff);
          $this->load->view('user_profile');
    }
#9

[eluser]fakeempire[/eluser]
Am I missing something? isnt that exactly what it will do with this code?

Code:
class Example extends Controller {
    
    function Example()
    {
         parent::Controller();
         if($this->session->userdata('logged_in') != 'yes'){
         $this->load->view('not_logged_in');
         return; // Maybe add this so it will stop immediately. -- maybe I am missing something.
         // why would it continue on to index -- isnt this code run first?
          }



         //logged in load other code
         // the above code is loaded for ever function inside the controller
    }
          // your functions here
    function index()
    {
          //i really don't want this code to be run...
          $this->db->insert('mytable',$some_dangerous_stuff);
          $this->load->view('user_profile');
    }
#10

[eluser]Majd Taby[/eluser]
This is exactly what hooks were made for.




Theme © iAndrew 2016 - Forum software by © MyBB