Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6584397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:31:57+00:00 2026-05-25T16:31:57+00:00

I want to generate global variables in CodeIgniter by creating my own library and

  • 0

I want to generate global variables in CodeIgniter by creating my own library and config file. This is what I wrote ini my library file, let’s say globalvars.php. I put it in /application/libraries.

class Globalvars{

    function __construct($config = array()) 
    {
        foreach ($config as $key => $value) {
            $data[$key] = $value;
        }
        $CI =& get_instance();
        $CI->load->library('session');
        $CI->load->vars($data); 
    }
}

I want the user id stored in the session to be available in global variable, so I wrote this in my config file. It’s named also globalvars.php. It’s in /application/config directory.

$config['user']=$this->session->userdata('id');

I then test to see if it’s working by write it in my controller this way.

echo $data['user'];

But I get this error in the browser

Message: Undefined property: CI_Loader::$session
Filename: config/globalvars.php

It seems that the session functions is not defined yet. How can I get it work? What have I missed here? Any help would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:31:57+00:00Added an answer on May 25, 2026 at 4:31 pm

    The config.php has to be loaded in order for the Session class to even be initialized, as it reads settings from that file.

    A lot of issues with this type of thing (setting some “global” data) can be resolved using a base controller, and extending it in your controllers.

    // core/MY_Controller.php
    MY_Controller extends CI_Controller {
    
        function __construct()
        {
            parent::__construct(); // Now the Session class should be loaded
            // set config items here 
        }
    }
    

    “Normal” controllers will now extend MY_Controller to take advantage of this.

    See: http://codeigniter.com/user_guide/general/core_classes.html for more details.

    In addition, when you load->vars(), they are available to the view layer only, it does not create a global variable called $data as you seem to be trying to access. If you do this:

    $this->load->vars(array('user' => '1'));
    

    You would access it in a file loaded by $this->load->view() like this:

    echo $user;  // outputs "1"
    

    See: http://codeigniter.com/user_guide/libraries/loader.html

    $this->load->vars($array)

    This function takes an associative array as input and generates
    variables using the PHP extract function. This function produces the
    same result as using the second parameter of the $this->load->view()
    function above. The reason you might want to use this function
    independently is if you would like to set some global variables in the
    constructor of your controller and have them become available in any
    view file loaded from any function. You can have multiple calls to
    this function. The data get cached and merged into one array for
    conversion to variables.

    I will say that as an experienced Codeigniter user, the concept of a “global vars” class is a bit wonky and probably unnecessary, especially when it’s already so easy to get and set config items. You could definitely run into some confusing issues and variable name conflicts with this method (pre-loading lots of view variables on every request).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a class of 30 students and want generate every possible
I want to generate a PDF file on server side, and then in response
I want to create an alias of @param for global variables used within a
I want to generate a thumbnail preview of videos in Java. I'm mostly JMF
I want to generate some XML in a stored procedure based on data in
I want to generate some formatted output of data retrieved from an MS-Access database
I want to generate a list in C#. I am missing python's list comprehensions.
I want to generate a dict with the letters of the alphabet as the
I want to generate random numbers manually. I know that every language have the
I want to generate random colours which can be attractive in pie charts.I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.