Welcome Guest, Not a member yet? Register   Sign In
User agent code for entire application

Hi there,

Quick question. I have about 4 lines of code that checks if the user agent is a mobile device or a normal browser. It then sets a variable, which I use to include the files necessary to build the view.

My entire application would need to be able to "see" this var but I'm not sure where to put the code so that the var is set when a page is requested then available to all the code throughout my app.

Does anyone have a suggestion on where the best place to put this (and similar code) is? Sorry if I'm not making sense!


if you set the value you want in the config object you will be able to access it anywhere
$this->config->set_item('my_value', $value);
then to read it
Just make sure that the name you use is unique so that it does not overwrite a core CI config value

Thanks for that, should I put that line in a certain file, or does it not matter where I put it?


It does matter to a certain degree where you put it
in a controller or model
$this->config->set_item('my_item', $value);
In a library, or if you must, a view
$CI =& get_instance();
$CI->config->set_item('my_item', $value);
Just be careful, if you are extending some of the core CI libraries, like Input, Router, Benchmark etc they may be instantiated before the controller is created in which case neither of the 2 methods above will work

Thanks for your help!

Theme © iAndrew 2016 - Forum software by © MyBB