CodeIgniter Forums

Full Version: Simple Acl
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi , when an user log into my site a set 2 session variable :

 $_SESSION['user_id] and $_SESSION['role_id']

to redirect every view if the user aren't logged i try to insert an if in the construct :

Code:
   public function __construct() {
       
      parent::__construct();
     
      $this->load->library('session');
     
      $this->config->load('user_login');
     
      $this->load->model('user_model');
        
         if(!isset($_SESSION['user_id'])){
             redirect('user/login');
         
         }else{
             redirect('user/index');

      }


   }

But  the browser tell me that ther'are too many redirect . Why ?
(09-02-2017, 03:41 AM)pippuccio76 Wrote: [ -> ]hi , when an user log into my site a set 2 session variable :

 $_SESSION['user_id] and $_SESSION['role_id']

to redirect every view if the user aren't logged i try to insert an if in the construct :

Code:
   public function __construct() {
       
      parent::__construct();
     
      $this->load->library('session');
     
      $this->config->load('user_login');
     
      $this->load->model('user_model');

if(!isset($_SESSION['user_id'])){
             redirect('user/login');
         
}else{
             redirect('user/index');

      }


   }

But  the browser tell me that ther'are too many redirect . Why ?

you have a redirect loop. the __construct is in the user class?

edit: if you not loged in. the redirect looks like user/index to user/index to user/index
(09-02-2017, 03:45 AM)Paradinight Wrote: [ -> ]
(09-02-2017, 03:41 AM)pippuccio76 Wrote: [ -> ]hi , when an user log into my site a set 2 session variable :

 $_SESSION['user_id] and $_SESSION['role_id']

to redirect every view if the user aren't logged i try to insert an if in the construct :

Code:
   public function __construct() {
       
      parent::__construct();
     
      $this->load->library('session');
     
      $this->config->load('user_login');
     
      $this->load->model('user_model');

if(!isset($_SESSION['user_id'])){
             redirect('user/login');
         
}else{
             redirect('user/index');

      }


   }

But  the browser tell me that ther'are too many redirect . Why ?

you have a redirect loop. the __construct is in the user class?

edit: if you not loged in. the redirect looks like user/index to user/index to user/index

The construct is in User controller.

If i am not logged the variable $_SESSION['user_id'] not isset and i redirect to user/login
(09-02-2017, 03:51 AM)pippuccio76 Wrote: [ -> ]
(09-02-2017, 03:45 AM)Paradinight Wrote: [ -> ]
(09-02-2017, 03:41 AM)pippuccio76 Wrote: [ -> ]hi , when an user log into my site a set 2 session variable :

 $_SESSION['user_id] and $_SESSION['role_id']

to redirect every view if the user aren't logged i try to insert an if in the construct :

Code:
   public function __construct() {
       
      parent::__construct();
     
      $this->load->library('session');
     
      $this->config->load('user_login');
     
      $this->load->model('user_model');

if(!isset($_SESSION['user_id'])){
             redirect('user/login');
         
}else{
             redirect('user/index');

      }


   }

But  the browser tell me that ther'are too many redirect . Why ?

you have a redirect loop. the __construct is in the user class?

edit: if you not loged in. the redirect looks like user/index to user/index to user/index

The construct is in User controller.

If i am not logged the variable $_SESSION['user_id'] not isset and i redirect to user/login

PHP Code:
if(!isset($_SESSION['user_id'])){
    
redirect('user/login');
 }else{
    
redirect('user/index');


the construct will be called automatically.

it check always the value and redirect you to the same constructor and it will redirect you.
(09-02-2017, 09:51 AM)Paradinight Wrote: [ -> ]
(09-02-2017, 03:51 AM)pippuccio76 Wrote: [ -> ]
(09-02-2017, 03:45 AM)Paradinight Wrote: [ -> ]
(09-02-2017, 03:41 AM)pippuccio76 Wrote: [ -> ]hi , when an user log into my site a set 2 session variable :

 $_SESSION['user_id] and $_SESSION['role_id']

to redirect every view if the user aren't logged i try to insert an if in the construct :

Code:
   public function __construct() {
       
      parent::__construct();
     
      $this->load->library('session');
     
      $this->config->load('user_login');
     
      $this->load->model('user_model');

if(!isset($_SESSION['user_id'])){
             redirect('user/login');
         
}else{
             redirect('user/index');

      }


   }

But  the browser tell me that ther'are too many redirect . Why ?

you have a redirect loop. the __construct is in the user class?

edit: if you not loged in. the redirect looks like user/index to user/index to user/index

The construct is in User controller.

If i am not logged the variable $_SESSION['user_id'] not isset and i redirect to user/login

PHP Code:
if(!isset($_SESSION['user_id'])){
 
   redirect('user/login');
 }else{
 
   redirect('user/index');


the construct  will be called automatically.

it check always the value and redirect you to the same constructor and it will redirect you.

Then how can i redirect if the user isn't logged ? Must i insert the if statement in every  controller's function ?
Is there  a way to do this  more quicly ?
Yes, Use a My_Controller and extend all of your Controllers from the MY_Controller

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

/**
 * ------------------------------------------------------------------------
 * Editor   : PhpStorm
 * Date     : 7/10/2017
 * Time     : 7:45 AM
 * Authors  : Raymond L King Sr.
 * ------------------------------------------------------------------------
 *
 * Class        MY_Controller
 *
 * @project     citest
 * @author      Raymond L King Sr.
 * @link        http://www.procoversfx.com
 * @copyright   Copyright (c) 2009 - 2017 Custom Software Designers, LLC.
 * @license     http://www.procoversfx.com/license
 * ------------------------------------------------------------------------
 */

/**
 * Class MY_Controller
 *
 * This file should be saved as:
 * ./application/core/MY_Controller.php
 *
 */
class MY_Controller extends CI_Controller {

    
/**
     * Class variables - public, private, protected and static.
     * --------------------------------------------------------------------
     */

    /**
     * @var  array
     */
    
protected $data = array();

    
/**
     * --------------------------------------------------------------------
     *  __construct
     * --------------------------------------------------------------------
     *
     * Class Constructor    PHP 5+
     *
     * @access    public
     */
    
public function __construct()
    {
        
parent::__construct();

        
/**
         * check to see if we want to use the CI profiler.
         * Requries the below in the ./application/config/config.php
         *
         * $config['profiler'] = FALSE;  // TRUE / FALSE
         */
        
$this->output->enable_profiler($this->config->item('profiler'));

        
log_message('debug''CI: MY_Controller class loaded');
    }

}    
// End of MY_Controller Class.


/**
 * Class Admin_Controller
 */
class Admin_Controller extends MY_Controller
{
    
/**
     * Class variables - public, private, protected and static.
     * --------------------------------------------------------------------
     */

    /**
     *  __construct ()
     * --------------------------------------------------------------------
     *
     * Class    Constructor
     */
    
public function __construct()
    {
        
parent::__construct();

    }


}    
// End of Admin_Controller Class.


/**
 * Class Public_Controller
 */
class Public_Controller extends MY_Controller
{
    
/**
     * Class variables - public, private, protected and static.
     * --------------------------------------------------------------------
     */

    /**
     *  __construct ()
     * --------------------------------------------------------------------
     *
     * Class    Constructor
     */
    
public function __construct()
    {
        
parent::__construct();

    }

}    
// End of Public_Controller Class.


/**
 * ------------------------------------------------------------------------
 * Filename: MY_Controller.php
 * Location: ./application/core/MY_Controller.php
 * ------------------------------------------------------------------------
 */