CodeIgniter Forums
Calling a function in many controllers - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Calling a function in many controllers (/showthread.php?tid=47527)



Calling a function in many controllers - El Forum - 12-12-2011

[eluser]veledrom[/eluser]
Hi,

I have a controller which has this validation function in it. Now I have many controllers and have to run this function all. I don't want to replicate same code over and over again all the controllers so please give me a hand to solve this problem. Where should I put this code and how do I call it etc...

Thanks

Code:
class Securedpage extends CI_Controller {

public function __construct()
{
   parent::__construct();  
   $this->site_authentication();
}

public function site_authentication()
{
   if($this->session->userdata('login_data'))
   {
      $login_data_array = $this->session->userdata('login_data');
  
      if(! isset($login_data_array['is_logged_in']) || $login_data_array['is_logged_in'] !== true)
      {
        redirect('loginout');
      }
   }  
   else
   {
     redirect('loginout');
   }
}
}