Hi all,
Noted that EE 2.0 has removed support of set_userdata in the session class. i.e.
http://ellislab.com/forums/viewthread/138599/
I just wondered if anyone had come up with a library to replace this? I already have a bespoke CI Auth class that uses set_userdata and want to use it on an EE project but don’t really want to replace all the CI session class based methods with native $_SESSION. Just thought I’d check to see if anyone has found a workaround already or written a class before I write one…!?
I would be interested in seeing this functionality restored in future builds of EE 2.0 in any case.
Thanks in advance!
Moved to Modules by Moderator
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.