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

  • SEARCH
  • Home
  • 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 8518841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:05:37+00:00 2026-06-11T06:05:37+00:00

What is the best practice of having dynamic globals in codeigniter? I want to

  • 0

What is the best practice of having dynamic globals in codeigniter?

I want to load a file with constants based on what key i load from the data
I tried libraries, but they are loaded before the models get initialized

How i want it to work:

1) User logs in
2) php checks if this user is a regular user (from database) or an administrator
3) if its a regular user, i will define my global variables accordingly
else i will use different global variables

This should happen only once, when person logs in. And this is exactly how i do it:

The person logs in, php checks (in same controller) how the global variables need to be defined. It defines them, but as soon as i call on these variables from another controller they get erased. (I call on another controller with AJAX)
Oh and i should also say that CI has bugs when we run multiple AJAX requests at the same time (you cannot use CI sessions if you use simultaneous AJAX)

SO the question really is: How do I prevent them from being erased?

Thanks

  • 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-06-11T06:05:38+00:00Added an answer on June 11, 2026 at 6:05 am

    First – the easy part:

    CodeIgniter database sessions with AJAX has been solved (very recently).

    Second – regarding globals:

    My advice for you is to first extend CodeIgniter’s CI_Controller class be implementing the techniques outlined on Phil Sturgeon’s blog entitled Keeping It Dry. Once this is completed, have all your controllers inherit from a MY_Controller instead of directly from CI_Controller.

    You can then have MY_In_Controller, MY_Out_Controller, MY_Admin_Controller…etc extend from MY_Controller, but I digress. The important part is to check for logged in status in your new base controller and set the globals.

    class MY_Controller extends CI_Controller{
        function __construct(){
            parent::__construct();
    
            $l = $this->session->userdata("loggedin");
            if((!isset($l))||($l==FALSE)){
                /*user is logged out - set appropriate globals*/
            }
            else{
                /*user is logged in - fetch them from the database and set appropriate globals*/
            }
        }
    }
    

    Now, when you create new controllers, say, Blog, News, or About, you won’t have to set the globals every time, they will be accessible in your controllers, models and views; all because the parent class will look after it.

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

Sidebar

Related Questions

The 'best practice' (as I see it) to atomically create a new file, is
What is the best practice for generating valid XML with PHP from user submitted
Having a discussion with a co-worker on what is best practice with CSS clear
I'm wondering what the best practice is for having redundant columns in a MySQL
What is the best practice to make changes in beans.xml file in production environment?
Coming from Java, I'm wondering if a Java best practice applies to JavaScript. In
Just to know the best practice or code efficiency, I am having this piece
I'm new to PHP and I'm having no luck googling for a best practice
I'm looking for the best practice for taking multiple span tags from multiple spans
I am having a very hard time finding a standard pattern / best practice

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.