This is a somewhat codeigniter/ee2.0 question. Hope it’s the right forum 😊
I want to do this in my plugin:
if($this->EE->session->userdata['language_code'])
{
$this->return_data = "Language is: " . $this->EE->session->userdata['language_code'];
} else {
$this->EE->session->userdata['language_code'] = "en";
}But I keep getting this error-message:
A PHP Error was encountered
Severity: Notice
Message: Undefined index: language_code
Filename: xxx/pi.xxx.php
Line Number: 44If i try wrapping it in “isset()” it doesn’t throw the error. But after the session variable language_code has been set upon reload the variable isn’t there anymore. It’s like it’s being flashed and the variable “language_code” is yet again empty.
Please help
Thank you
Moved to Plugin by Moderator, title changed
Hi
I’m trying to add something to the userdata session variable but it doesn’t seem to stick. I’ve just put in a simple code to try it:
function code()
{
$code = "Code before is: " . $this->EE->session->userdata['code'];
$this->EE->session->userdata['code'] = "en";
$code .= "Code after is: " . $this->EE->session->userdata['code'];
return $code;
}The first line tells me there’s an undefined index in the userdata array. The second line gives me back “en” as it should.
When I then reload the site the first line should give me “en” back as well since it’s been added to the session, but it doesn’t. It’s empty.
– I already have a thread about this (http://ellislab.com/forums/viewthread/138599/) but haven’t recieved an answer in over 2 days so I thought maybe it was higher activity in this forum for EE2.0 related questions. Sorry if I broke some rule with my double post, if so feel free to remove one thread. Thank you.
I don’t believe that EE stores the session->userdata in session variables. I think it regenerates the array on each page load after verifying the user’s session.
What you probably want is Session Cache
I also had a similar thread but didn’t really get an official answer. That was initially regarding flashdata - which as far as I can tell now, works ONLY on the control panel side, which isn’t clear at all from the user guide. Too bad, as flashdata would be really handy on the front-end.
As far as the session cache goes, that won’t do it - per the manual:
$this->EE->session->cache is an array provided for you to use for “flash” content, i.e. values that you would like to persist during a page load …
Emphasis mine. It only works during page loads, not between page loads (I can confirm this).
I haven’t had a chance to try this yet, but CodeIgniter 1.7.2 uses set_userdata:
$data = array(
'username' => 'johndoe',
'email' => '[email protected]',
'logged_in' => TRUE
);
$this->EE->session->set_userdata($data);Give that a try.
Yes I can also confirm that the session->cache doesn’t work for more then one pageload.
“set_userdata” doesn’t work either. When using it EE gives you:
Fatal error: Call to undefined method EE_Session::set_userdata() in /../system/expressionengine/libraries/Functions.php(614) : eval()'d code on line 18When looking over the functions.php file the “set_userdata” functions seems to have been removed.
So right now you can’t develop any third-party plugins, extensions or modules using sessions for the frontend?
Could we get some official response to how to do this? Maybe we have missed something in the docs.
Thanks
Okay… I haven’t even got pb2 installed but I’m gonna take a stab at it anyway.
Inside of a plug-in:
class Some_plugin {
var return_date = '';
function Some_plugin() {
$this->EE =& get_instance();
}
// set the flash data to have on next page load {exp:some_plugin:set_flash lang="en"}
function set_flash($var_name, $var_value) {
$var_name = 'lang';
$var_val = $this->EE->TMPL->fetch_param('lang');
$this->EE->session->set_flashdata($var_name, $var_value);
return;
}
// get the flash data for session on this page load
function get_flash($var_name) {
$this->return_data = $this-EE->session->flashdata($var_name);
return this->return_data;
}
// keep the flash data for the next page load {exp:some_plugin:keep_flash var='lang'}
function keep_flash($var_name) {
$var_name = $this->EE->TMPL->fetch_param('var');
$this->EE->session->keep_flashdata($var_name);
return;
}
}Reference:Public Beta Plug-in docs, Public Beta Session Class, Code Ignitor Session Docs
I’m not sure weather you can make a reference to the EE super object within the template parser, so I’m not sure you can do this as php in a template; however, plug-ins allow you to step out of the template, do your thing, then step back in.
Hope this is useful and… well, right. Since, this is straight of the top of my head.
Well it’s right if you want to utilize the flashdata and keep having to set/get it all the time.
Our question right now is more HOW do you utilize the session class? This is why I asked for this thread to be put back into “Technical Support” since it’s more of a general question regarding sessions then how to use it within a plugin since the practice pretty much will be the same for plugins, modules and extensions.
The CI documentation regarding the session class can’t be used since for example “set_userdata” seems to have been removed completly.
// On a different note I tried running session_start() and then use the regular $_SESSION[] variable which works but seems like a big dirty workaround.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.