CodeIgniter Forums
Login class - 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: Login class (/showthread.php?tid=36550)



Login class - El Forum - 12-06-2010

[eluser]Unknown[/eluser]
Hello,
I want to create a global function that checks if the user is logged in and redirect to specific page without calling it in every page

any idea?

thanks

Yaron


Login class - El Forum - 12-06-2010

[eluser]flaky[/eluser]
something like this could be done, there are some other ways also

Code:
//put this class in the application/libraries folder
class Application extends Controller {

    public function __construct() {
        parent::__construct();
        
        if(!$this->session->userdata('user_id'))
            redirect("user/login");
    }

}

//this one is one of your controllers in the application/controllers
class Page extends Application {

    public function __construct() {
        parent::__construct();
        
        //your constructor code
    }
    
    public function index() {
        //your code
    }

}



Login class - El Forum - 12-06-2010

[eluser]LuckyFella73[/eluser]
Like Flaky posted extending the CI Controller is a good way
to do get the wanted result.

A more detailed post about that can be found on Phil Sturgeons block:
http://philsturgeon.co.uk/news/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY