At some point during the user’s session (some page load), you acquire the need to use a language code (I’m guessing to serve the appropriate language). From this point you wish to trigger the storage of this information for future use during the user’s session. Since you are trying to tie this to the user’s session, I am assuming you do not wish to retrieve this information beyond the current session.
The discovery of the information will trigger the storage of the information.
I am assuming that the session->userdata array is constructed from a number of sources including database queries. It would appear that there is probably no session variable userdata; therefore, there is no process within the construction of the array to check for or append to this array any information that may or may not have been stored as session variables (flash or otherwise).
This means that you would have to code the process for appending the array on each page load. Which brings us to the method I suggested of passing the information to append and then appending the array using an extension using the session_end hook.
Though this does not answer the question of why the information needs to be held in the userdata array. Why couldn’t you just as easily store it in the session->cache where it really belongs, since it will only have meaning to your plug-ins, modules, etc.
Problem with session->cache is as with session->flashdata that it’s only available during one, or as with fd two, pageload(s).
The discussion has more come to the point where we are asking WHERE do we store session variables thats suppose to be able to hold throughout the entire user session (like seesion_id or similuar).
Let’s say I have something I want to store in a session variable for 1929239293 pageloads, like a setting. Where do I do this? userdata seems to be locked (or re-generated upon each pageload). Cache and flashdata are only temporarly.
Get my question/wondering/request? 😊
Unless they rewrote the session library from CI, EE2 is not storing anything as a session variable; but, rather serializing the array and stuffing it into a database.
So on page load it retrieves the serialized array from the db, creates the userdata array, marks all flash data as “old”.
set_flashdata adds that item to the userdata array marked as “new” and will serialize it and stuff it into the database for next session request.
keep_flashdata replaces the “old” flag with a “new” flag and preserves the information for next session request.
At the end of page load processing it “sweeps” the flashdata marked as old.
So, as I see it you have three options.
An extension that applies a keep_flashdata to all your exising flashdata.
Write and session variables directly through the $_SESSION.
Submit a feature request to have a way to preserve data over the length of a session.
I know this is an old thread and I will attempt to track down some newer ones to post this to as well. Anyway there is a better workaround for this than writing a library that uses PHP sessions. You can use the CI Session library in your Plugin library folder.
Copy Session.php found in ./system/libraries to ./system/expressionengine/third_party/plugin_name/libraries/
Then rename the file to something else (I made mine “Good_session.php”) Make sure you also rename the class and constructor to the same name. So in my case its
class Good_Session {
...
function Good_Session($params = array()) {
...Now there is one more issue that would need to be addressed and I will explain it. The CI Session library will end up using the EE->input->cookie method to access the session cookie the problem with this is that unlike the CI->input->cookie method the EE method adds the cookie prefix (usually “exp_”) to the front of the cookie. So when loading our library we need to specify that the EE cookie prefix be added, making our load look like this:
$this->EE->load->library("good_session", array('cookie_prefix'=>'exp_',
'sess_cookie_name'=>'sess_cookie',
'cookie_path'=>'/'));So now when the cookie is written it will be named exp_sess_cookie. Which would be great but now when our library attempts to read the cookie it will use the same EE->input->cookie method that will again add the cookie prefix. So when the library attempts to read the cookie it will look for a cookie named “exp_exp_sess_cookie”. To get around this we need to edit our session library again. The first line in the sess_read method looks like this:
$session = $this->CI->input->cookie($this->sess_cookie_name);You should change it to look like this:
$session = $this->CI->input->cookie(substr_replace($this->sess_cookie_name, '', 0, strlen($this->cookie_prefix)));That will remove our cookie prefix so that it can be added back in the input->cookie method.
So to recap: Copy the CI Session library to your plugin libraries directory Rename file, class, and constructor to something else. Change sess_read method to remove cookie prefix Load library with the same cookie prefix that is defined in EE.
Do that and you should have a working CI session library. Which will let you do all the cool stuff that CI lets you do and it will be separate from the EE session library so there shouldn’t be any issues with overlap.
A few minor notes any config options you wish to use with the session library should be passed in using the load method with parameters. Also I have only used the set_userdata, userdata, and unset_userdata methods so I don’t know how the others work but I would imagine they work without issue.
Hopefully EE will let us have access to the session at some point but until then you can use this.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.