Welcome Guest, Not a member yet? Register   Sign In
Load function on every page
#1

[eluser]comyou[/eluser]
Hello all,

I current want to run a function to check if the logged in user is an admin everytime a new page is loaded.

My function is
Code:
function check_admin() {
    $logged_in = $this->session->userdata('logged_in');
    if ($logged_in == TRUE) {
        $this->get_where_custom('username', 'admin');
        foreach($query->result() as $row) {
            if ($row->num_rows() == 0) {
            $this->session->unset_userdata('is_admin');
            $this->session->set_userdata('is_admin', FALSE);
            } else {
            $this->session->unset_userdata('is_admin');
            $this->session->set_userdata('is_admin', TRUE);
            }
        }
    }
}

However I'm not sure if this is working properly but I could always fix that once it loads on every page.
The function is found in my 'application/modules/secure/security.php'

I've tried putting it in a helper file (but being honest, I wasn't really sure what I was doing.

How would I load the function when every page loads?
Thanks in advanced.
#2

[eluser]CroNiX[/eluser]
Create a base controller. The base controller does the auth. Every other controller that needs the auth checked would extend that base controller. CI has one base controller that can be used as default.

create /application/core/MY_Controller.php

Code:
class MY_Controller extends CI_Controller {
  function __construct()
  {
    parent::__construct();
    //do your auth check here...
  }
}

Then your other controllers extend MY_Controller....

Code:
class Other_controller extends MY_Controller {
//anything in this class will do auth very first thing since it loads MY_Controller first
}

If you have a controller that you don't want to use auth on, then have that controller extend the regular CI_Controller as normal.
#3

[eluser]comyou[/eluser]
EDIT:
Sorry, derp moment.
I'm already loading a third_party MX_Controller so I needed to add my function to that.

Thanks for solving it.




Theme © iAndrew 2016 - Forum software by © MyBB