i have a function for login controls. if user didnt login it redirects to login.php
in my controller i have to many functions and this functions represents pages in website. so do i have to call $this->is_logged_in();
function in each function.
for example:
class Admin extends CI_Controller{
function index(){
$this->is_logged_in(); // works fine like this
$this->load->view('admin/welcome_message');
}
function users(){
$this->is_logged_in(); // works fine like this
$this->load->view('admin/users');
}
function comments(){
$this->is_logged_in(); // works fine like this
$this->load->view('admin/comments');
}
}
i dont want to call this function in all function. when i call this in construct result is: infinite loop.
Create your own base controller in your application/core folder and in its constructor do:
Then all of your controllers just need to inherit your new controller and then all requests will check that the user is logged in.
This can be more specific if required by also checking
$this->router->method
to match against a specific action.Add this constructor, and you wont have to write that function on each.
Define who is parent and check the function result. Done
The best way to do this kind of things is to use hooks Use session data in hooks in codeIgniter
that will be all you should need.