Welcome Guest, Not a member yet? Register   Sign In
How to protect admin pages?
#2

(03-14-2015, 04:40 PM)lexxtoronto Wrote: So when people try to access a page it should check if they are logged in and if not they will be redirected to the login page. How does this checking happen?

One way to do that is to put the checking code in the controller's constructor. For example, in an application I am building, I only require that people be logged in. So in every controller's constructor, I put code to check for that. Like this.
PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Gifts extends CI_Controller {

 
 public function __construct()
 
 {
 
   parent::__construct();
 
   $this->load->helper ('url');
 
   if ( ! $this->session->userdata('user_id'))
 
   {
 
     redirect('user/login');
 
     exit;
 
   }
 
   $this->load->helper ('form');
 
   $this->load->library ('form_validation');
 
   $this->load->library ('table');
 
   $this->load->model('gifts_model');
 
   $this->load->model('glist_model');
 
 

I load the URL helper because I need it for the possible redirect. Then I check to see if the session has a 'user_id,' indicating a logged-in user. If not, I redirect to the login page and exit. Otherwise, if the user is logged in, I continue to load the rest of my helpers, libraries, and models.

I hope that helps. Smile
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply


Messages In This Thread
How to protect admin pages? - by lexxtoronto - 03-14-2015, 04:40 PM
RE: How to protect admin pages? - by RobertSF - 03-14-2015, 06:10 PM
RE: How to protect admin pages? - by lexxtoronto - 03-14-2015, 06:39 PM
RE: How to protect admin pages? - by RobertSF - 03-14-2015, 06:57 PM
RE: How to protect admin pages? - by lexxtoronto - 03-14-2015, 07:06 PM



Theme © iAndrew 2016 - Forum software by © MyBB