Running functions on every page load

#1
[eluser]xtremer360[/eluser]
I have seen a few sites that talk about how to make a an is logged in function to have it automatically run after every page load without having to call the function in every controller and wondered if anyone can point me to one of them because I lost the links to the few I saw.

#2
[eluser]CroNiX[/eluser]
base controller (MY_Controller)

#3
[eluser]xtremer360[/eluser]
Is there a link that can explain this further because that tells me nothing.

#4
[eluser]PhilTem[/eluser]
http://lmgtfy.com/?q=codeigniter+my_controller

http://philsturgeon.co.uk/blog/2010/02/C...ing-it-DRY

#5
[eluser]xtremer360[/eluser]
Thank you Phil. I took a look at the second link and believe I understand this for the most part.

I created a MY_Controller that extends the CI_Controller as well as Frontend_Controller that extends the MY_Controller which controls the main site of my website and I also have a Backend_Controller that controls my CMS I am creating.

I also have a Login controller that extends teh Backend_Controller. The question I have is what if I want to run an if statement for the whole backend area to see if there is a session variable isset and if so then check to see if the value is numeric. If it is then have them redirect to the login and if not then have them redirect to the control panel controller which also extends the Backend_Controller. Do I do this if statement inside the Backend_Controller?

#6
[eluser]PhilTem[/eluser]
If there are any functions or any business logic to be performed on calls to specific groups of controllers (i.e. frontend, backend, ...) then you would put these function calls or business logic into the __construct() method so it will get performed whenever the controller is instantiated.

#7
[eluser]xtremer360[/eluser]
So your saying I can do something like this. If yes, then since my login controller extends the backend controller then it will run that if statement, correct?

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Backend_Controller extends MY_Controller {

public function __construct()
{
  parent::__construct();
  
  if (isset($this->session->userdata('xtr')) && is_integer($this->session->userdata('user_variable_id')))
  {
   redirect('login');
  }
  else
  {
   redirect('cpanel'};
  }
}
}

#8
[eluser]xtremer360[/eluser]
Would I do the opposite.

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Backend_Controller extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
  
        if (!!isset($this->session->userdata('xtr'))) && (!is_integer($this->session->userdata('user_variable_id'))))
        {
            redirect('login');
        }
    }
}

#9
[eluser]CroNiX[/eluser]
[quote author="xtremer360" date="1355062599"]Is there a link that can explain this further because that tells me nothing.[/quote]It's not my job to do your research for you. I gave you the terms needed to search on. Phil took it one step more and entered the terms into google for you because it's too hard for you to do that apparently. Maybe next time I can fly to you and load it on your pc for you so you don't even have to do that much.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.