CodeIgniter User Authentication
The best way is to split your logic into two (or more) controllers.
- A 'front end' controller - where the user does not need to be loggedin for ANY of it
- A 'back end' controller(s) - where the user MUST belogged in for ALL of it
In your backend controller just do this
class Backend extends CI_Controller { public function __construct() { parent::__construct(); if( ! ($this->admin_model->logged_in()) { // Not logged in - so force them away redirect ('place login page here'); } } }
Then EVERYTHING in backend
controller is protected.
To take this concept further - look into using a MY_Controller
and get all your backend controllers to extend from this.
Either I misunderstood your question, or the solution is very simple.
function books_page() { if(!$this->admin_model->logged_in()) { redirect('auth/login'); ## OR $this->load->view("error_page"); exit(); } ## All your code, etc.}
I would declare a variable on top of the controller like this:
private $logged_in = false;
Then in the constructor I would initialise it like so:
$this->logged_in = $this->session->userdata('logged_in');
Then you can disable the access to desired methods (or the complete controller if you put your check in the constructor):
if($this->logged_in){ //do stuff}else{ redirect(base_url().'login');}