CodeIgniter Forums
Can i check userlogin in construct ? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: CodeIgniter 2.x (https://forum.codeigniter.com/forumdisplay.php?fid=18)
+--- Thread: Can i check userlogin in construct ? (/showthread.php?tid=381)



Can i check userlogin in construct ? - bobykurniawan - 11-27-2014

PHP Code:
class Admin extends CI_Controller {
function 
__construct()
{
 
   parent::__construct();
 
      $aut $this->donaturlib->is_admin();
 
   if(!$aut)
 
   {
$msg="Mohon Maaf anda tidak memiliki akses";
$this->session->set_flashdata('donatur',$msg);
redirect('welcome');
 
   }
 
   $this->link ='login/out';
$this->info ='Logout';
}
function 
index()
{
$data['link']=$this->link;
$data['info']=$this->info;
$data['include']= 'default';
$this->load->view('a_template/main.php',$data);    
}


Up there is my script, i'm trying to check the user login in function __construct. It's working fine, but is it 'in the right way'?


RE: Can i check userlogin in construct ? - sv3tli0 - 11-27-2014

Its ok if you need the client to be logged for all methods inside this controller..


RE: Can i check userlogin in construct ? - bobykurniawan - 11-28-2014

Thank you for your answer


RE: Can i check userlogin in construct ? - unodepiera - 11-28-2014

(11-27-2014, 03:46 AM)bobykurniawan Wrote:
PHP Code:
class Admin extends CI_Controller {
function 
__construct()
{
 
   parent::__construct();
 
  $aut $this->donaturlib->is_admin();
 
   if(!$aut)
 
   {
$msg="Mohon Maaf anda tidak memiliki akses";
$this->session->set_flashdata('donatur',$msg);
redirect('welcome');
 
   }
 
   $this->link ='login/out';
$this->info ='Logout';
}
function 
index()
{
$data['link']=$this->link;
$data['info']=$this->info;
$data['include']= 'default';
$this->load->view('a_template/main.php',$data); 
}


Up there is my script, i'm trying to check the user login in function __construct. It's working fine, but is it 'in the right way'?


I think the best way is to use hooks, tutorial in spanish(http://uno-de-piera.com/uso-de-hooks-en-codeigniter), regards.


RE: Can i check userlogin in construct ? - bobykurniawan - 11-28-2014

i can't read in spanish haha


RE: Can i check userlogin in construct ? - sv3tli0 - 11-29-2014

Hooks v 2.2 http://www.codeigniter.com/user_guide/general/hooks.html

Hooks v 3.0 http://www.codeigniter.com/userguide3/general/hooks.html

I personally prefer extending the CI Controller instead of using a hook about this but it can be a solution if you decide to use it.


RE: Can i check userlogin in construct ? - ivantcholakov - 11-29-2014

CI_Controller -> Base_Admin_Controller (implement here the user check) -> Admin (the called by the router controller)

AJAX requests may also require authentication, for this case I create lighter Base_Admin_Ajax_Controller, with user check again.