CodeIgniter Forums
Help: Where to put my controller function logged_in() - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Help: Where to put my controller function logged_in() (/showthread.php?tid=39747)



Help: Where to put my controller function logged_in() - El Forum - 03-19-2011

[eluser]Wondering Coder[/eluser]
I have this function in almost all of my controllers, this prohibit any unauthorized user to access a certain page in my site.
Code:
function is_logged_in()
    {
        $is_logged_in = $this->session->userdata('is_logged_in');
        if(!isset($is_logged_in) || $is_logged_in != true)
        {
            echo 'You don\'t have permission to access this page. <a href="../login">Login</a>';    
            die();        
            //$this->load->view('login_form');
        }        
    }

But what I would like to do is just call this function logged_in in just one controller and not doing a copy-paste thing. Is this the part where I would need to extend my controller? Haven't really tried it though. BTW - It's just been 3 months since I learned using CI.


Help: Where to put my controller function logged_in() - El Forum - 03-19-2011

[eluser]WanWizard[/eluser]
Read up on base controllers.

Phil Sturgeon wrote a good article about it.

In short, create an Public_Controller, Private_Controller and maybe an Admin_Controller. All extend CI_Controller. All your controllers that need to be accessable without authentication will extend Public_Controller, all controllers that require you to be logged in extend Private_Controller, which checks the logged in state. And you may have other base controller for specific functions.

You also use these base controllers to house all methods and properties that should be available to all your controllers, so you'll have to code them only once.


Help: Where to put my controller function logged_in() - El Forum - 03-19-2011

[eluser]Wondering Coder[/eluser]
thnx WanWizard,

I'm reading it right now. Actually I have 2 module site: Admin Module and Private Module and as for my public site maybe its just for my registration and login page.. I'm getting the picture now. I'm getting the picture now.

Also does this article up to date? Because I'm using CI 2.0.


Help: Where to put my controller function logged_in() - El Forum - 03-19-2011

[eluser]Wondering Coder[/eluser]
follow up question WanWizard,

Code:
application/libraries/Admin_Controller.php
this part confused me. Why do I need to save the Admin_Controller in the application/libraries not in the application/core just like the Public_Controller? Because I already extended my CI library to MY_Controller in the application/libraries. Or maybe I'm misunderstood something here?


Help: Where to put my controller function logged_in() - El Forum - 03-19-2011

[eluser]WanWizard[/eluser]
CI doesn't have any class autoloading capabilities, so in theory you can put them where you want them.
You have to load them manually before the controller gets loaded.

As of CI 2.0, I would suggest putting them in core, together with MY_Controller. For CI 1.7.x, libraries is the place to store them.


Help: Where to put my controller function logged_in() - El Forum - 03-19-2011

[eluser]Wondering Coder[/eluser]
ahh, ok. Its just that based from what I understand all extended library in system/libraries had to be put in application/libraries. hmmn.

Anyway thanks a many WanWizard ^_^.