1

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.

2
  • Does your website use session data? Maybe declaring globals could work? Commented Dec 30, 2013 at 20:44
  • yes i use session data , Sorry i want to store a JSON object to be precise... Commented Dec 30, 2013 at 21:08

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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;

Comments

0

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

Session docs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.