gouthaman93
gouthaman93

Reputation: 290

codeigniter global variable need to be accessed by multiple controllers

I am new to codeigniter , In my program i want a variable need to be accessed by multiple controllers, It's not a constant variable, value of variable changes ,

Sorry , My mistake I want to store a JSON object to be precise

Pls help me to figure this out.

Thanks in advance.

Upvotes: 1

Views: 388

Answers (3)

MonkeyZeus
MonkeyZeus

Reputation: 20737

Option 1

Since you are using CodeIgniter and sessions then something like this could work out for you:

set it

$someJSONobject = 'JSON';

$this->session->set_userdata('GLOBAL_JSON', $someJSONobject);

retrieve it

$someJSONobject = $this->session->userdata('GLOBAL_JSON');

echo $someJSONobject->subitem;

Make sure you are storing sessions in a DB if you go with this option because Cookie space is VERY limited

Option 2

Even if you are not using CodeIgniters' session implementation then you can do something quite similar in native PHP:

$someJSONobject = 'JSON';

$_SESSION['GLOBAL_JSON'] = $someJSONobject;

Upvotes: 0

Momin
Momin

Reputation: 868

Appending on Rooneyl's solution you may want to save that value on session which is easier to access from all end

Session docs

Upvotes: 0

Rooneyl
Rooneyl

Reputation: 7902

You can create a base controller with an attribute for your variable, then have your controllers extend that base controller.

Upvotes: 2

Related Questions