I have seen a few sites that talk about how to make a an is logged in function to have it automatically run after every page load without having to call the function in every controller and wondered if anyone can point me to one of them because I lost the links to the few I saw.
I found this website that talks about how to achieve this:
philsturgeon.co.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY
I created a MY_Controller that extends the CI_Controller as well as Frontend_Controller that extends the MY_Controller which controls the main site of my website and I also have a Backend_Controller that controls my CMS I am creating.
I also have a Login controller that extends teh Backend_Controller. The question I have is what if I want to run an if statement for the whole backend area to see if there is a session variable isset and if so then check to see if the value is numeric. If it is then have them redirect to the login and if not then have them redirect to the control panel controller which also extends the Backend_Controller. Do I do this if statement inside the Backend_Controller?
This is what I I thought up.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Backend_Controller extends MY_Controller
{
public function __construct()
{
parent::__construct();
if (!!isset($this->session->userdata('xtr'))) && (!is_integer($this->session->userdata('user_variable_id'))))
{
redirect('login');
}
}
}
Is this correct way to achieve this and the right place to do so? If so, would I do anything specific to the login controller or the other controllers for managing users?
Or is there a better idea to work with this?