Where do I put a function that I want to execute on every page load?

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.

Sounds like a library method to me. Autoload it if you don't want to call it.

&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends Controller {

function MY_Controller() {

echo "echo every page this text"

You need then from controller extends MY_Controller.

Any advice on which of these two approaches is preferred?

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.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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