Welcome Guest, Not a member yet? Register   Sign In
Where to put a "global variable" for the entire website


I am making a site with the name "This is my temp name" and the only way I have found to pass this name in the a content view is with a $data['name'] = "This is my temp name". It happens I have multiple controllers where I have to do the same thing, then if the namesite changes, I will have to change it in different places.

I'm sure there is a way to declare the sitename in just one place and make the call to this variable from any controller, but I don't how. Maybe in the config file?

How do you do this?

You're correct, stick it in your config.php file:

$config['sitename'] = 'This is my temp name';

then you can use it anywhere in your app with the following:


thanks kgill
I knew it would be something really simple.

here is another idea:
If you only need the $name variable just show up to the view (e.g. the banner or title of website), try create a View file (call it header.php or so) then call it from the controller:
$this->load->view('contents', $data);
$this->load->view('footer');            // there you can add footer, and guess what? maybe some navigation panel?

Thanks chakhar86.
I was doing that.

What if you need a global variable whose value comes from a model?
Wherever the variable is set, I need to be able to pass $CI->session->userdata('user_id') to the model's method.

$cur_user = $CI->UsersModel->get_user_data($CI->session->userdata('user_id'));

I've tried from the config, but its not letting me perform $CI->session->userdata('user_id'). (I'm assuming because the session lib hasn't been loaded at that point, but I don't know.)

I found a temporary solution.

I've created this library which is autoloaded.
It loads the UsersModel and creates a key in the config just like I want.
Now I can call $this->config->item('cur_user_level') in my controllers.
Is this effective? If not, how should I do it?

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

* Class is auto-loaded by CI.
class AuthInit {

    var $CI;

    function AuthInit()

    * Selectively set config items that contain the logged in user's data.
    function init()

        $user_level    = $this->CI->UsersModel->get_user_level($this->CI->session->userdata('user_id'));

        $this->CI->config->set_item('cur_user_level', $user_level);

    function _get_ci()
        $this->CI =& get_instance();

// end of file

simshaun why not add the user level to the session userdata once the user logs in instead of fetching on every page load and storing it in the config class?


use auto-load library

open system/application/config/autoload.php
register libraries/helpers/models that you want to load automatically (e.g. session lib)

chakhar86, simshaun is autoloading the library. But that is not the main problem.

The main problem is the library gets loaded when the user_id is in the session or not, making database calls when they aren't needed.

The cur_user_level data is only useful when a user is logged in or you have to see the not logged user as a user_level. but that can be checked as follows
if( ! $this->session->userdata('cur_user_level'))
   // not logged in
    // do something with cur_user_level here

ah, I think I misunderstood simshaun intention...

So you are saying that if 'user_data' session hasn't registered then you shouldn't make a database call instead just bring the user to login form?

Theme © iAndrew 2016 - Forum software by © MyBB