How to check if a user is registered?

I am using the CodeIgniter framework for PHP . There are several pages that are intended exclusively for the administrator, and they are located in Admin/* . When a user logs in, I save some value in the session as a flag and check it in my controller to check if the user is registered or not. I wrote code to verify the session in each method of my controller. But then I realized that I did not want to write the same line of code in each method, since many problems are created from the point of view of ease of maintenance. Then I decided to create an exclusive controller that will load only the views of the administrator and, therefore, in it the constructor, I will check the value of the session. Is there any other method besides this approach. Am I doing it right? Or is any other secure mechanism available in CodeIgniter ?

+4
source share
3 answers

You took one of the best approaches (my opinion), just add other admin controllers from this controller so that you can have specialized controllers (admin blog, admin gallery, etc.). If you need help, I will gladly help you.

+4
source

you could do it in your constructor method something like this,

 function __construct { parent::construct(); /* Do you login check here */ } 
+1
source

For instance:

 class Admin extends Controller { function __construct() { parent::__construct(); $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'); } } 
+1
source

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


All Articles