0

I'm almost embarrassed to ask because it seems so simple, but I can't get it to update.

When the user logs in I set the session vars like array('users'=>array('timezone'=>'America/los Angeles'));

I can then retrieve the data as follows: $_SESSION['users']['timezone']

and it works fine.

However in the user profile page the user can change their timezone and when I try to update the $_SESSION as follows it doesn't work:

$_SESSION['users']['timezone'] = 'America/Denver';

What am I doing wrong?

--- More code as requested -------

I found that the session variables were being set by a function inside of a class

Here's the function:

function session_var_register($object_name, $var_value)
    {
        $_SESSION[$object_name]=$var_value;
    }

Here's how the function got called:

$gf->session_var_register("users", $user_array)

Users Array looks like array('users'=>array('timezone'=>'America/los Angeles'));

I don't understand why this doesn't work. I was able to get around the problem though by bypassing the function call and just creating the array like:

$_SESSION['users'] = $user_array;

But for knowledge reasons and if anyone else comes along this post, could anyone explain what the function was doing different? There were no errors thrown, just would not allow me to assign anything to the session variable once it was registered via the function...almost acted like it became read_only once instantiated.

5 Answers 5

2

Make sure you session_start() on every page that accesses the $_SESSION variable.

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

3 Comments

Thanks Kris. I caught the mix up after I submitted and have edited to fix the answer. I can never keep PHP function names straight :)
thanks. checked it and session is started...var_dump($_SESSION) showed everything...but still can't assign to it?
You may want to show more of your code then. There's no reason it shouldn't work based on what you've described so far.
0

Sounds like the code that updates it may not be getting executed. You might try putting some kind of debugging statement before this assignment like an echo to verify that the action is being taken.

1 Comment

did that and it is executing to that statement, but just doesn't assign to the session.
0

Following on from Scott's reply, double checking your session is started is a good "start".

There's a good tip here which you may find useful in debugging.

Comments

0

re-initialize your session id. That way you are sure it has a new spanking id to store your variables.

Comments

0

Are you doing a redirect soon after this code?

Do a session_write_close() before doing a redirect so that session vars are stored again before redirecting.

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.