CodeIgniter Forums
Making code run on all pages - 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: Making code run on all pages (/showthread.php?tid=27612)



Making code run on all pages - El Forum - 02-16-2010

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


Making code run on all pages - El Forum - 02-16-2010

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


Making code run on all pages - El Forum - 02-16-2010

[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?


Making code run on all pages - El Forum - 02-16-2010

[eluser]laytone[/eluser]
Yes