Setting and Retrieving Current Variables

My elgg installation allows a user to create separate spaces and to use objects within each space independently.  A new object should use the space as its default container.  I think I would like to set the space in the session variable and retrieve it using a function.  Unfortunately, I don't know how.

Can you tell me how to set the space in the session and retrieve it using a (new) function like "get_current_space()"?  I don't know how I should do either of those things.

Maybe there's a better way?  I'm certainly open to recommendations.

  • You can retrieve the current session of the user by calling `elgg_get_session()` this will result in an \ElggSession object.

    You can set any value you wish by doing the following

    $session = elgg_get_session();
    $session->current_space_id = 12335;

    To use the stored variable simply use

    $session = elgg_get_session();
    $current_space_id = $session->current_space_id;

    Hope this helps


  • "Session" might not be what I was looking for.  Elgg's session object does not seem to store the current_space_id for AMD requests between page refreshes.  Is there a way to store the variable more persistently?  Would using a JSON object be advisable?  I need good advice as much as I do answers.

  • I don't know enough to know if there's any way to save a session id persistently over AMD requests.

    What about using cookies? Would that be a possibility to save a session id on the client side to identify the session?

  • I considered cookies, but I don't think they're as reliable as I would like.  People can disable cookies in their browser. Isn't the JSON option like server-side cookies? I went this route and it seems to be working.  An added bonus is being able to persist across sessions so one can resume in the space that one left last time.

    Are there concerns about using the JSON approach?  It seems pretty secure, but I'm not a student of security best- practices.