Welcome Guest, Not a member yet? Register   Sign In
Where do I put a function that I want to execute on every page load?
#1

[eluser]smick[/eluser]
Hey Guys,

Been using CI for a while now but there is one thing I still don't understand.

Say I have a script that I need to execute on every page of the site, before I load my view files or call my controllers, where do I put it?

I'm working with an ip to location script and I need to establish the user's location so I can use the data in my controller.

The script checks to see if the user's location is stored in a session, if not it calls a web service and retrieves it. I want the session data to always be available. In other words, every time I want to call it I don't want to have to call a helper, just $location = $this->session->userdata('location');

Thanks for any help. It's probably obvious, but I'm just not sure how to go about it.
#2

[eluser]WanWizard[/eluser]
Sounds like a library method to me. Autoload it if you don't want to call it.
#3

[eluser]kkristo[/eluser]
<code>
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends Controller {

function MY_Controller() {
parent::Controller();

echo "echo every page this text"

}
}
</code>
You need then from controller extends MY_Controller.
#4

[eluser]pbreit[/eluser]
Any advice on which of these two approaches is preferred?
#5

[eluser]WanWizard[/eluser]
It's entirely up to you. Whatever you feel most comfortable with.

You can even mix-n-match, and add simple things or methods unique to your application to your MY_Controller, and methods you feel could be reusable to a library which you call from MY_Controller.




Theme © iAndrew 2016 - Forum software by © MyBB