Welcome Guest, Not a member yet? Register   Sign In
Can i check userlogin in construct ?
#1

(This post was last modified: 11-27-2014, 03:47 AM by bobykurniawan.)

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'?
Reply
#2

Its ok if you need the client to be logged for all methods inside this controller..
Best VPS Hosting : Digital Ocean
Reply
#3

Thank you for your answer
Reply
#4

(This post was last modified: 11-28-2014, 11:24 AM by unodepiera.)

(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.
Reply
#5

i can't read in spanish haha
Reply
#6

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

Hooks v 3.0 http://www.codeigniter.com/userguide3/ge...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.
Best VPS Hosting : Digital Ocean
Reply
#7

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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB