How can I use the __construct function in my other CodeIgniter controller

I have a controller called member inside this build function

 function __construct() { parent::Controller(); $this->is_logged_in(); } 

I want to check my other controller that the user is registered, how can I use this function in my other controller named profile and others

This is my first project with CodeIgniter

+4
source share
3 answers

Your authentication should be in the library:

This is an excerpt from the basic authentication codigniter script:

 class Site_sentry { function Site_sentry() { $this->obj =& get_instance(); } function is_logged_in() { if ($this->obj->session) { if ($this->obj->session->userdata('session_logged_in')) { return TRUE; } else { return FALSE; } } else { return FALSE; } } function login_routine() { //do login here (enter into session) } } 

This library is stored in the application / libraries under the file name, named after its class with the suffix .php.

Then you can add this to your autoload application / conig / config.php configuration file:

 $autoload['libraries'] = array('database', 'site_sentry', 'session'); 

or load it manually in each controller:

 $this->load->library('Site_sentry); 

Then you can check your session from inside the controllers, for example:

  class Class extends Controller{ function Class() { parent::Controller(); if( $this->site_sentry->is_logged_in() == FALSE){ redirect('managerlogin/'); } } } 

Also check out this documentation page http://codeigniter.com/user_guide/libraries/sessions.html ; Of particular interest is saving a session to a database partition.

+3
source

I don’t think that doing it with the class is the best idea. If the user is logged in, you should check the flag (value or something else) inside the session , so you do not need to work with another controller.

The advantage is that access to the session can be easier, and this is a more general approach.

0
source

Session Example:

 class SomeClass extends Controller { function __construct() { parent::Controller(); $this->is_logged_in(); } function is_logged_in() { $is_logged_in = $this->session->userdata('is_logged_in'); if(!isset($is_logged_in) || $is_logged_in != TRUE) { redirect('login'); } } 
0
source

Source: https://habr.com/ru/post/1315483/


All Articles