CodeIgniter Forums

Full Version: Making code run on all pages
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]oste15[/eluser]
Hello,

I am wondering what the best way to run code on each page without having to add it into each controller.

For example if you want to check if a user is logged in on each page, and send them to the login page if they are not logged in. You could add the following to each controller, but I think it would be best to just write this once and run it on each page no matter what.

Code:
if(!$user){
//send to login
}

Thanks for clearing this up for me.

El Forum

[eluser]gh0st[/eluser]
Put it in MY_Controller. Then you extend MY_Controller. Do a search on the topic of MY_Controller both here and Google and you will get some good pointers.

El Forum

[eluser]oste15[/eluser]
So when I create this MY_Controller class I will have to extend that class rather than Controller in each of my controllers?

El Forum

[eluser]laytone[/eluser]
Yes